I'm trying to get a list of all contents of a directory, targetDir
, including files and subdirectories. For this I am using
from pathlib import Path
targetDir = r"\\sharedDrive\folder1\folder2\folder3"
allTargets = Path(targetDir).rglob("*")
However, this returns the following error:
FileNotFoundError: [WinError 3] The system cannot find the path specified: '\\\\sharedDrive\\folder1\\folder2\\folder3\\[...]\\folder10'
My best guess is that this is caused by Windows' 260 character path limit. The path mentioned in the traceback is 257 characters long. I have redacted the path for privacy.
Troubleshooting:
I know the path exists, because I can see it in File Explorer. Moreover, dir "\\sharedDrive\folder1\folder2\folder3\[...]\folder10"
returns
10/26/2020 12:01 PM <DIR> .
10/26/2020 12:01 PM <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 367,916,392,448 bytes free
I can do
os.listdir("\\sharedDrive\folder1")
and continue down each folder all the way to folder9
:
os.listdir("\\sharedDrive\folder1\folder2\folder3\[...]\folder9")
But at folder10
I get the same FileNotFoundError: [WinError 3]
error.
Also, when I do
i = 0
j = 0
allTargets = Path(targetDir).rglob("*.*")
for _ in allTargets:
i += 1
I get i
as 27 817.
I'm doing this on Anaconda's IPython and Windows CMD Python:
Python 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.29.0 -- An enhanced Interactive Python. Type '?' for help.
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
References
I've consulted these SO posts: