stat doesn't work!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paulo Pinto

    stat doesn't work!

    I am doing an application that needs to
    know when it is manipulating symbolic links.

    But it is failing to reckognize them.

    If I run the following code:

    <code>
    import sys
    import os
    from stat import *

    mode = os.stat(sys.arg v[1])[ST_MODE]
    if S_ISLNK(mode):
    print 'File is a link'
    else:
    print 'Just a normal file'
    </code>


    I will alway get the 'Just a normal file'
    message even though I am using a symbolic
    link as argument.

    I've done the same example in Perl and in
    bash and it works.

    Could this be a bug in my Python version
    (2.2.2) or I am doing something wrong ?

    Thanks in advance,
    Paulo Pinto

  • Jeff Epler

    #2
    Re: stat doesn't work!

    On Mon, Feb 09, 2004 at 05:10:31PM +0100, Paulo Pinto wrote:[color=blue]
    > I am doing an application that needs to
    > know when it is manipulating symbolic links.[/color]

    try os.lstat.
    [color=blue][color=green][color=darkred]
    >>> import os
    >>> from stat import *
    >>> S_ISLNK(os.stat ("/proc/self").st_mode)[/color][/color][/color]
    0[color=blue][color=green][color=darkred]
    >>> S_ISLNK(os.lsta t("/proc/self").st_mode)[/color][/color][/color]
    1

    Jeff

    Comment

    • Donn Cave

      #3
      Re: stat doesn't work!

      In article <c08bdn$fq6$1@s unnews.cern.ch> ,
      Paulo Pinto <paulo.pinto@ce rn.ch> wrote:[color=blue]
      > I am doing an application that needs to
      > know when it is manipulating symbolic links.[/color]

      That's a special case, and therefore you have
      to use a special version of stat(). stat()
      will follow the link, so you get the original
      file. lstat() will report the link itself.

      Donn Cave, donn@u.washingt on.edu

      Comment

      • Paulo Pinto

        #4
        Re: stat doesn't work!

        It works now.
        Thanks for your answers.

        Paulo Pinto wrote:[color=blue]
        > I am doing an application that needs to
        > know when it is manipulating symbolic links.
        >
        > But it is failing to reckognize them.
        >
        > If I run the following code:
        >
        > <code>
        > import sys
        > import os
        > from stat import *
        >
        > mode = os.stat(sys.arg v[1])[ST_MODE]
        > if S_ISLNK(mode):
        > print 'File is a link'
        > else:
        > print 'Just a normal file'
        > </code>
        >
        >
        > I will alway get the 'Just a normal file'
        > message even though I am using a symbolic
        > link as argument.
        >
        > I've done the same example in Perl and in
        > bash and it works.
        >
        > Could this be a bug in my Python version
        > (2.2.2) or I am doing something wrong ?
        >
        > Thanks in advance,
        > Paulo Pinto
        >[/color]

        Comment

        Working...