Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileNotFoundError on long pathname in Python in Windows

Tags:

python

I have run into an issue while working with numpy's savetxt function. I have been trying to save data deep inside a file structure. Below is a minimal working example (for you to run it you will need to make the following directories, however).

import numpy as np

dir1 = "LongDirectoryName/"
dir2 = "VeryLongDirectoryName/"
dir3 = "EvenLongerDirectoryName/"
dir4 = "LongestOfThemAllDirectoryName/"

filename = "../" + dir1 + dir2 + dir3 + dir4 + "longfilename_with_129_charss.txt"   # this works
#filename = "../" + dir1 + dir2 + dir3 + dir4 + "longfilename_with_130_charsss.txt" # this does not 
print(len(filename))
myarray = np.array([1,2,3])
np.savetxt(filename, myarray)

The relative filepath is not an issue because the 129 character filename does work. It seems to me that 129 characters is the limit. When I go to try the 130 character filename, I receive the following error:

File "C:\Users\njkro\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 1359, in savetxt
    open(fname, 'wt').close()
FileNotFoundError: [Errno 2] No such file or directory: '../LongDirectoryName/VeryLongDirectoryName/EvenLongerDirectoryName/LongestOfThemAllDirectoryName/longfilename_with_130_charsss.txt'

Other Information
I am using Windows 10.
I am using Anaconda, and the version of numpy I am using is 1.16.2.

My questions:

  1. Can anyone confirm this?
  2. Is there an explanation for this character limit? I would understand a 128 character limit more than 129.
  3. Is there a workaround (for long filenames) if I can't change the file system?
like image 880
Nathaniel Kroeger Avatar asked Oct 24 '25 13:10

Nathaniel Kroeger


1 Answers

It appears that Windows produces an error based on the absolute path. Even if you try to get into a deeply nested directory by chdir'ing in steps, it will fail if the absolute path is too long.

You can still get there by resorting to the FAT32-style 8-character filename aliases (even when on NTFS), for example, rather than

open('abcdefghiabcdefghiabcdefghi/abcdefghiabcdefghiabcdefghi', 'w')

you do

open('ABCDEF~1/ABCDEF~1', 'w')

but it is rather involved to reliably get these short filenames.

Alternatively: change a registry setting to allow long path names on this particular Windows system (use regedit.exe): under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem, set LongPathsEnabled to DWORD value 1.

like image 141
Han-Kwang Nienhuys Avatar answered Oct 26 '25 03:10

Han-Kwang Nienhuys