Search code examples
pythonpathsaveioerror

IOError with Python write but the directory exists


I'm trying to open and write to a file which may or may not exist. I'm have Windows 7 and am using Python. I'm getting an IOError because the file was not found. Here is my code to save my file:

dirBool = os.path.exists(saveDir)
print dirBool
if not dirBool:
    os.mkdir(saveDir)

if saveDir == os.path.dirname(newFname):
    print 'They are the same'
else:
    print 'They are not the same'

print saveDir
print newFname
fileSpace = open(newFname, "w")

In another part of my code I created newFname with os.path.join(saveDir, fname), with fname being what you will see below in the output. The output I get is:

True
They are the same
//itsofs04.itap.purdue.edu/bio_mousevision/Data/skissing/WT vs Fragile X/FXS Paper/16.02.9 4 WT 4 FX VEH vs DGX/16.02.9 CC#028849 Group1B ET#387 pre t/Pupilometry Data_1.2
//itsofs04.itap.purdue.edu/bio_mousevision/Data/skissing/WT vs Fragile X/FXS Paper/16.02.9 4 WT 4 FX VEH vs DGX/16.02.9 CC#028849 Group1B ET#387 pre t/Pupilometry Data_1.2\010 G-1-G-2-G Drifting 0.0625s Interval_2016-02-09_18-08-04_units_010 Video_pupilometry_1.2_x_y_Area.hdf5

I am aware that these are long names but it's required. You can see that the directory both exists, and that it is the same as the directory that the new file will be saved into. The error I get is:

IOError: [Errno 2] No such file or directory: u'//itsofs04.itap.purdue.edu/bio_mousevision/Data/skissing/WT vs Fragile X/FXS Paper/16.02.9 4 WT 4 FX VEH vs DGX/16.02.9 CC#028849 Group1B ET#387 pre t/Pupilometry Data_1.2\\010 G-1-G-2-G Drifting 0.0625s Interval_2016-02-09_18-08-04_units_010 Video_pupilometry_1.2_x_y_Area.hdf5'

Things I've tried so far:

  1. Change forward slashes to backward slashes
  2. Change only some of the forward slashes and/or some of the backslashes
  3. Type cast newFname to str
  4. Get rid of any files in the directory that come close to what newFname is called.

I can't think of anything else to do, nor why it would be throwing me that error in the first place.


Solution

  • When using network drives with Windows, the drive has to be mapped to a drive letter. This can be done by right clicking My Computer > Map Network Drive. After that, use the mapped drive letter in the path to open().