FAQ
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 ?

Search Discussions

  • Christian Heimes at Aug 28, 2008 at 10:11 pm

    saswat at gmail.com wrote:
    File symLinkTest is a symbolic link.

    Why S_ISLNK(mode) returns False and S_ISREG(mode) returns True ?
    Because you are using os.stat() instead of os.lstat().

    http://docs.python.org/lib/os-file-dir.html

    Christian
  • Saswat at Aug 28, 2008 at 11:41 pm

    On Aug 28, 3:11?pm, Christian Heimes wrote:
    sas... at gmail.com wrote:
    File symLinkTest is a symbolic link.
    Why S_ISLNK(mode) returns False and S_ISREG(mode) returns True ?
    Because you are using os.stat() instead of os.lstat().

    http://docs.python.org/lib/os-file-dir.html

    Christian
    Do you mean the following is deprecated ?
    http://docs.python.org/lib/module-stat.html
    From the documentation -
    S_ISLNK( mode)
    Return non-zero if the mode is from a symbolic link.

    Thanks
    -Saswat
  • Miles at Aug 29, 2008 at 5:06 am

    saswat wrote:
    On Aug 28, 3:11 pm, Christian Heimes wrote:
    sas... at gmail.com wrote:
    File symLinkTest is a symbolic link.
    Why S_ISLNK(mode) returns False and S_ISREG(mode) returns True ?
    Because you are using os.stat() instead of os.lstat().
    Do you mean the following is deprecated ?
    http://docs.python.org/lib/module-stat.html
    From the documentation -
    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
  • Fredrik Lundh at Aug 29, 2008 at 5:20 am

    saswat at gmail.com wrote:

    Do you mean the following is deprecated ?
    http://docs.python.org/lib/module-stat.html
    From the documentation -
    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>
  • Saswat at Aug 29, 2008 at 8:16 am

    On Aug 28, 10:20?pm, Fredrik Lundh wrote:
    sas... at gmail.com wrote:
    Do you mean the following is deprecated ?
    http://docs.python.org/lib/module-stat.html
    From the documentation -
    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>
    Thank you Fredrick and Miles,

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

    -Saswat

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 28, '08 at 9:55p
activeAug 29, '08 at 8:16a
posts6
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase