Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if a directory is a windows junction in python

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

like image 782
watashiSHUN Avatar asked Jan 29 '23 04:01

watashiSHUN


1 Answers

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
like image 164
Demez Avatar answered Jan 31 '23 19:01

Demez