Python directory question

06-14-2019 07:34 AM
New Contributor II

import arcpy
from arcpy import da
import os

attachTable = 'C:\users\user\Test2\pictures.gdb\pictures__ATTACH' # Table in GDB holding attatchments
origTable = 'C:\Users\user\Test2\pictures.gdb\pictures' # Layer in GDB holding features to which attatchments belong
nameField = 'Name' # Field where you want to save the photos
fileLocation = 'C:\Users\user\pictures\pics' # folder where you want to save the photos

# creates a cursor to search through the attachment tables; specify yu only wish to search those three fields
attachCursor = arcpy.da.SearchCursor(attachTable, ['DATA', 'ATT_NAME', 'REL_GLOBALID'])

# Begin searching the table and storing the actual images, movies, etc.
for attRow in attachCursor:
binaryRep = attRow[0]
filename = attRow[1]
relID = attRow[2]

# creates a cursor to sort the features; essentially to find a match fro the relID above
originCursor = arcpy.da.SearchCursor(origTable, ['GLOBALID', nameField])
for origRow in originCursor:
origID = origRow[0]
origName = origRow[1]
if origID == relID:

# Saves a file in the specified location tht contains the name chosen by the user for the attachment
open(fileLocation + os.sep + origName + ".jpg", 'wb').write(binaryRep.tobytes())

# deletes search cursor
del originCursor


Traceback (most recent call last):
File "C:/Users/mpretorius/Desktop/", line 28, in <module>
open(fileLocation + os.sep + origName + ".jpg", 'wb').write(binaryRep.tobytes())
IOError: [Errno 2] No such file or directory: u'C:\\Users\\user\\pictures\\pics\\HGC-2.jpg'


Can't seem to figure out what is wrong in this script. Any help appreciated! 

0 Kudos
6 Replies
MVP Regular Contributor

Hi Marco Pretorius,

The quickest solution would be to replace all backward slashes in your directory paths to forward ones.

So 'C:\users\user\Test2\pictures.gdb\pictures__ATTACH' should become 'C:/users/user/Test2/pictures.gdb/pictures__ATTACH', etcetera.

And then save and run the script again 🙂

Does this solve your issue?



0 Kudos
New Contributor II

Getting this error 

Traceback (most recent call last):
File "C:/Users/user/Desktop/", line 28, in <module>
open(fileLocation + os.sep + origName + ".jpg", 'wb').write(binaryRep.tobytes())
IOError: [Errno 2] No such file or directory: u'C:/Users/user/pictures/pics/\\HGC-2.jpg'

Any further suggestions?

0 Kudos
MVP Regular Contributor

The issue might be caused by the two backslashes in the filename: C:/Users/user/pictures/pics/\\HGC-2.jpg

You should try to find out where these come from.

MVP Esteemed Contributor

The Python open command will not be able to create a directory if it doesn't exist, just a file, so make sure all of the directories in the path exist prior to running that command.

0 Kudos
MVP Legendary Contributor

for line numbers to facilitate references to script lines see


MVP Legendary Contributor

You should have gotten failures way before you did.

You need to raw encode your paths little 'r'

p = 'C:\users\user\Test2\pictures.gdb\pictures__ATTACH'
  File "<ipython-input-2-3d412528dd89>", line 1
    p = 'C:\users\user\Test2\pictures.gdb\pictures__ATTACH'
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \uXXXX escape

p = r'C:\users\user\Test2\pictures.gdb\pictures__ATTACH'  # --- raw encoded

# use all forward slashes
0 Kudos