5

In os there's a function os.path.islink(PATH) which checks if PATH is symlink. But if fails when PATH is a symlink to some directory. Instead -- python thinks it is directory (os.path.isdir(PATH)). So how do I check if a dir is link?

Edit:

Here's what bash thinks:

~/scl/bkbkshit/Teaching: file 2_-_Classical_Mechanics_\(seminars\)
2_-_Classical_Mechanics_(seminars): symbolic link to `/home/boris/wrk/tchn/2_-_Classical_Mechanics_(seminars)'

and here's what python thinks:

In [8]: os.path.islink("2_-_Classical_Mechanics_(seminars)/")
Out[8]: False
4

1 回答 1

13

This happens because you put a slash at the end of the filename.

os.path.islink("2_-_Classical_Mechanics_(seminars)/")
                                                  ^

The trailing slash causes the OS to follow the link, so that the result is the target directory which is not a link. If you remove the slash, islink will return True.

The same thing happens in Bash as well:

g@ubuntu:~$ file aaa
aaa: symbolic link to `/etc'
g@ubuntu:~$ file aaa/
aaa/: directory
于 2013-03-30T11:33:40.903 回答