0

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?

576i
  • 7,579
  • 12
  • 55
  • 92

1 Answers1

0

I ended up using os.walk instead if pathlib's glob which ran through the whole drive without problems.

Problably because the default behaviour of os.walk is to ignore errors

import os

# replacement for
# for f in Path(drive_path).glob("**/*"):

drive_path = "e:\\"

for root, dirs, files in os.walk(drive_path):
    for file in files:
        f = Path(root, file)
        # do somethin
        print(f.name)
576i
  • 7,579
  • 12
  • 55
  • 92