I am trying to walk through all files on an "exFAT" formatted drive on Windows 10
from pathlib import Path
def scan_drive(drive_letter):
for f in Path(f"{drive_letter}:\\").glob("**/*"):
# do something, code removed, print instead
print(f.name)
result = scan_drive('E')
When this code reaches a long filepath name, in my example 264 characters, the job crashes with a FileNotFound error.
File "C:\data\PyCharmProjects\drivescan_py\src\scan_drive.py", line 85, in scan_and_dump
result = scan_drive(f'{drive_letter}:\\')
File "C:\data\PyCharmProjects\drivescan_py\src\scan_drive.py", line 38, in scan_drive
for f in Path(drive_path).glob("**/*"):
File "C:\Program Files\Python39\lib\pathlib.py", line 1167, in glob
for p in selector.select_from(self):
File "C:\Program Files\Python39\lib\pathlib.py", line 601, in _select_from
for p in successor_select(starting_point, is_dir, exists, scandir):
File "C:\Program Files\Python39\lib\pathlib.py", line 548, in _select_from
with scandir(parent_path) as scandir_it:
FileNotFoundError: [WinError 3] Das System kann den angegebenen Pfad nicht finden: 'E:\\laptop\\c\\ProgramData\\..."
My guess is that Windows can't read paths over 256 bytes length this way.
I'm looking to just ignore long paths (and other errors, like permissions) in a glob using pathlib so that I can scan through the rest of the files.
Can I fix this using pathlib without writing my own walker?