Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if directory is symlink?

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
like image 860
Adobe Avatar asked Mar 30 '13 11:03

Adobe


People also ask

How do you check if a directory is a symbolic link bash?

Your Bash script might need to determine if a file is a symlink or not. In Bash you can test this with the -L operator that returns true if the file exists and is a symlink.

How do you tell if a folder is a symbolic link Windows 10?

In Command Prompt, run this command: dir /AL /S c:\ A list of all of the symbolic links in the c:\ directory will be returned.

Is a symlink a directory?

Symlink, also known as a symbolic link in Linux, creates a link to a file or a directory for easier access. To put it in another way, symlinks are links that points to another file or folder in your system, quite similar to the shortcuts in Windows.


1 Answers

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
like image 114
interjay Avatar answered Oct 14 '22 11:10

interjay