I am running os.walk()
on "C:\Users\confusedDev\Documents"
, I see ["My Music", "My Pictures"...]
returned as subDirs but they are not actually being visited. After some research I found that they are actually junctions in Windows.
My understanding is that junction is a symlink points to directory, which gets ignored by default during os.walk()
, but the following test has failed me
>>> os.path.islink("C:\\Users\\confusedDev\\Documents\\My Pictures")
False
hmm...how did os.walk()
know that "C:\Users\confusedDev\Documents\My Pictures" is a symlink to "C:\Users\confusedDev\Pictures" and needed to be skipped? I want to call the same api...For now, my workaround logic simply assumes that if a directory is skipped by os.walk()
, it is a junction
A bit late, but you can wrap os.readlink() in a try and except for directory junctions, and then put that in a function you could call, like this:
def is_junction(path: str) -> bool:
try:
return bool(os.readlink(path))
except OSError:
return False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With