Hi, everyone again.
I’ve made code for get list files in folder and subfolders.
It’s working if full path to the file less than 256 symbols.
If full path more than 256 symbols ( (Windows issue). ), I would get error “FileNotFoundError: [WinError 3]” System can’t find file.
Could you please healp me how to avoid it ???
Code
import os
import csv
directory = r"y:\GIRS"
with open(‘C:/Temp/20_MDG_Congo_MarineXII/opa2.csv’, ‘w’) as f:
write = csv.writer(f)
for root, dirs, files in os.walk(directory):
for file in files:
result = (file,os.path.join(root,file),os.stat(os.path.join(root,file)).st_size)
print (result)
write.writerow(result)
As you implied, this is a limitation of Windows itself, which is kept to preserve backwards compatibility with programs. But Python is compiled to be able to handle longer paths, if you enable the relevant registry setting. After that your program will just work.
Dear Spencer,
Thank you so much.
Your suggest is correct for Windows 10 or 11.
I have Windows 7 and there isn’t working in section “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem”
I can change code ??? I don’t know how to do it.
The limit is 257 made up of 2 for device “c:” etc the 255 for the path.
Which comes from the maximum size of a pascal string that used a single byte for its length.
Isn’t windows cute?
To be fair, that’s on par with how the size of the Space Shuttle’s solid rocket boosters were governed by the width of two horses’ behinds. The original reason for it is really just history, and it’s been the maintenance of backward compatibility and standards that have kept it the same ever since.