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
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.
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.
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.
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
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