Checking if the file is a symlink fails

S

saswat

check if file is a symlink
Here is a small piece of code:

import os
from stat import *

filePath = "/home/xyz/symLinkTest"
mode = os.stat(filePath)[ST_MODE]

print 'Find using os.path : ',os.path.islink(filePath)

print 'Find using mode :', S_ISLNK(mode)
print 'Is this a regular file : ' S_ISREG(mode)

Output :
---------

Find using os.path : True
Find using mode : False
Is this a regular file : True


File symLinkTest is a symbolic link.

Why S_ISLNK(mode) returns False and S_ISREG(mode) returns True ?
 
M

Miles

saswat said:
Do you mean the following is deprecated ?
http://docs.python.org/lib/module-stat.html


S_ISLNK( mode)
Return non-zero if the mode is from a symbolic link.

No, it means that os.stat follows the symbolic link and gives stat
information for the file that the symbolic link points to, which is
*not* a symbolic link. Like the documentation for lstat says: "Like
stat(), but do not follow symbolic links." But why not just use
os.path.islink?

-Miles
 
F

Fredrik Lundh

Do you mean the following is deprecated ?
http://docs.python.org/lib/module-stat.html


S_ISLNK( mode)
Return non-zero if the mode is from a symbolic link.

As that page states, that's a function used to interpret a mode flag
returned by os.stat, os.fstat, or os.lstat. It obviously won't give you
the result you're looking for if you use a stat function that *follows*
symbolic links.

</F>
 
S

saswat

As that page states, that's a function used to interpret a mode flag
returned by os.stat, os.fstat, or os.lstat.  It obviously won't give you
the result you're looking for if you use a stat function that *follows*
symbolic links.

</F>

Thank you Fredrick and Miles,

stat() is on the actual file and not on the symlink. That explains.

-Saswat
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top