How to test if a file is a symbolic link?

ma mabdelkader at gmail.com
Wed Oct 28 23:56:21 EDT 2009


import os

if os.path.islink('symbolic_link'):
     print "hello."

Cheers,
Mahmoud Abdelkader

On Oct 28, 2009, at 11:19 PM, Peng Yu <pengyu.ut at gmail.com> wrote:

> 'symbolic_link' is a symbolic link in the current directory. I run
> 'python main.py', but it does not return me anything. I want to check
> if a file is a symbolic link. I'm wondering what is the correct way to
> do so?
>
> $cat main.py
> import stat
> import os
>
> st = os.stat('symbolic_link')
> if stat.S_ISLNK(st.st_mode):
>  print "Hello"
> -- 
> http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list