0

I have checked the directory exists, and I am using Pathlib

As far as I can tell, I have used the same syntax successfully, but for some lines only the code fails with a file does not exist error:

Just one example of failure

print(data_folder.is_dir()) #when I run, returns True

with (data_folder / "lensingtimedelayswithinimage.npy" ).open(mode="w+b") as lensingtimedelaysfile: 
     np.save(lensingtimedelaysfile, np.array(tdwithinimage))

and returns the error

FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\angel_000\Documents\ArchiveofDocuments\2019\New_Time_Delay_Code\Thin_Inclined_Ring\Rmax_200_fracinc_0p5\200\LCDM\zs_3_zl_1_c_299792458_H0_75\beta_0p065_0p065\Softened_ellipsoidal_isothermal\b_1_s_0p1_eps_0p1_rotangle_0_centre_00\1001\im0\lensingtimedelayswithinimage.npy'

another example in the same code

data_folder_main.mkdir(parents=True, exist_ok=True)

try:
    if (data_folder_main / "imagepositions.npy").stat().st_size == 0:
        raise ValueError('Empty file')
    with (data_folder_main / "imagepositions.npy").open(mode="rb") as imagepositionsfile:
        theta_all_arraylist = np.load(imagepositionsfile)        
except (FileNotFoundError, ValueError) as error:
    theta_all_arraylist = lensmodel.theta_all_arraylist(betaarray.astype('float64'))
    with (data_folder_main / "imagepositions.npy").open(mode="w+b") as imagepositionsfile: #save the image position of the particles
        np.save(imagepositionsfile, theta_all_arraylist)  #this runs fine each time      
    with (data_folder_main / "imagepositionscentre.npy").open(mode="w+b") as imagepositionscentrefile: save the image position of the particles
        np.save(imagepositionscentrefile, theta_centresource) #this fails each time with file not found
mallowcodes
  • 45
  • 1
  • 7

1 Answers1

1

The example you gave (path plus file name) is 280 characters long, which exceeds the maximum length of file names on Windows (around 260 characters, see https://stackoverflow.com/a/265785/9794932 for example). Please try using a shorter file name or path (or both!).

Rob Bricheno
  • 4,467
  • 15
  • 29