How to test if a file is a symbolic link?

D'Arcy J.M. Cain darcy at druid.net
Wed Oct 28 23:49:04 EDT 2009


On Wed, 28 Oct 2009 22:19:55 -0500
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

As soon as you import os the OS that you are running is relevant.  I'll
assume some sort of Unix.

> st = os.stat('symbolic_link')

The os module uses the underlying OS calls.  run "man 2 stat" to see
what stat does.  I think you will find that lstat is what you want.

-- 
D'Arcy J.M. Cain <darcy at druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.



More information about the Python-list mailing list