os.lstat : proper way to do this

Discussion in 'Python' started by m1k3b0@gmail.com, Dec 12, 2008.

  1. Guest

    I'm converting from Perl to Python, so I'm learning the basics -
    please be gentle! :)

    In Perl, I can lstat($file) whether $file exists or not:
    lstat($file);
    unless (-e _) {
    print STDERR "$file: No such file or directory\n";
    }
    unless (-l _) {
    print STDERR "$file: Not a symbolic link\n";
    }

    The lstat() returns normally whether the file exists or not, and I
    check for existence with the "-e" conditional. Then I go on and check
    other modes.

    In Python,if I os.lstat(file) and file doesn't exist, I get an error
    from within the module:
    OSError: [Errno 2] No such file or directory: '/etc/xxx'

    I can check first with os.path.exists(file), but then I'm doing two
    stat() type calls which is inefficient (especially when we're talking
    thousands of files).

    I want to be able to do something like this:
    mode = os.lstat(file)[ST_MODE]
    if not mode:
    print >> sys.stderr, file, ": No such file or directory"
    if not S_ISLNK(mode):
    print >> sys.stderr, file, ": Not a symbolic link"

    Of course, this isn't valid. How can I do this efficiently without
    getting "no such file" error?

    Thanks!
    mikebo
    , Dec 12, 2008
    #1
    1. Advertising

  2. MRAB Guest

    wrote:
    > I'm converting from Perl to Python, so I'm learning the basics -
    > please be gentle! :)
    >
    > In Perl, I can lstat($file) whether $file exists or not:
    > lstat($file);
    > unless (-e _) {
    > print STDERR "$file: No such file or directory\n";
    > }
    > unless (-l _) {
    > print STDERR "$file: Not a symbolic link\n";
    > }
    >
    > The lstat() returns normally whether the file exists or not, and I
    > check for existence with the "-e" conditional. Then I go on and check
    > other modes.
    >
    > In Python,if I os.lstat(file) and file doesn't exist, I get an error
    > from within the module:
    > OSError: [Errno 2] No such file or directory: '/etc/xxx'
    >
    > I can check first with os.path.exists(file), but then I'm doing two
    > stat() type calls which is inefficient (especially when we're talking
    > thousands of files).
    >
    > I want to be able to do something like this:
    > mode = os.lstat(file)[ST_MODE]
    > if not mode:
    > print >> sys.stderr, file, ": No such file or directory"
    > if not S_ISLNK(mode):
    > print >> sys.stderr, file, ": Not a symbolic link"
    >
    > Of course, this isn't valid. How can I do this efficiently without
    > getting "no such file" error?
    >

    Just catch the exception:

    try:
    mode = os.lstat(file).st_mode # An alternative way.
    # If we get here then the file does exist.
    if not S_ISLNK(mode):
    print >> sys.stderr, file, ": Not a symbolic link"
    except OSError, e:
    if e.errno == 2:
    # File doesn't exist.
    print >> sys.stderr, file, ": No such file or directory"
    else:
    # Failed for some other reason.


    BTW, "file" happens to be the name of one of the build-ins, so try to
    use something else, eg. "file_path".
    MRAB, Dec 12, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Chermside

    RE: lstat(fileName)[ST_SIZE]

    Michael Chermside, Jun 10, 2005, in forum: Python
    Replies:
    0
    Views:
    413
    Michael Chermside
    Jun 10, 2005
  2. Adrian Petrescu

    Interpreting os.lstat()

    Adrian Petrescu, Jul 19, 2007, in forum: Python
    Replies:
    9
    Views:
    606
    =?ISO-8859-15?Q?=22Martin_v=2E_L=F6wis=22?=
    Jul 21, 2007
  3. Giampaolo Rodola'

    Is os.lstat available on all platforms?

    Giampaolo Rodola', Nov 29, 2007, in forum: Python
    Replies:
    2
    Views:
    240
    Giampaolo Rodola'
    Nov 29, 2007
  4. onLINES

    lstat() and stat()

    onLINES, Sep 30, 2008, in forum: C Programming
    Replies:
    4
    Views:
    429
    Joachim Schmitz
    Sep 30, 2008
  5. alberttresens
    Replies:
    3
    Views:
    644
    Thomas Jollans
    Jul 26, 2010
Loading...

Share This Page