I am trying to return a list of all files and subfolders in a particular location. My code is as follows:
from pathlib import Path
FOLDER_PATH = Path(r'C:\long\file\path\of\138\characters\')
I get the error:
FileNotFoundError: [WinError 3] The system cannot find the path specified:
The error occurs on a folder path, not a file, so I'm not sure if that could be the reason.
When I go into the folder manually and try to open the PDF
in there, I get "There was an error opening this document. This file cannot be found."
Similarly, when I try to open the XLSX
file, I get "This file could not be accessed. Try one of the following: (make sure it exists, isn't read only, isn't more than 218 characters, etc.)"
The file paths in this folder are certainly more than 218 characters, which I understand can be an issue for Excel, but I don't understand why it would be in issue for pathlib.Path.rglob
to list them, does anyone understand this?
However, if I use CMD
(dir /s /b > files.txt
) I am able to get the list.
Additionally, if I then import files.txt
into a list of Path
objects, paths
, in python and try to do [x.is_file() for x in paths]
, it will not properly identify some of the longer paths as files.
I have verified that if I copy the directory locally (where a much shorter path exists) that the files are accessible by Excel and pathlib.Path.rglob
.
What can be done to work around this issue, and why is it an issue in the first place?