How to test if a file is a symbolic link?

ma mabdelkader at
Thu Oct 29 04:56:21 CET 2009

import os

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

Mahmoud Abdelkader

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

> 'symbolic_link' is a symbolic link in the current directory. I run
> 'python', 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
> import stat
> import os
> st = os.stat('symbolic_link')
> if stat.S_ISLNK(st.st_mode):
>  print "Hello"
> -- 

More information about the Python-list mailing list