Testing File Existence - best way?

Discussion in 'Perl Misc' started by still just me, Dec 13, 2007.

  1. I need to test if a file exists. Is there a better technique than
    "attempt open and watch for an error"? The program will be a cgi
    running in a windows or linux environment.

    Thanks,
    still just me, Dec 13, 2007
    #1
    1. Advertising

  2. still just me wrote:
    > I need to test if a file exists. Is there a better technique than
    > "attempt open and watch for an error"? The program will be a cgi
    > running in a windows or linux environment.


    Why not use the "file exists" operator? See

    perldoc -f -e

    jue
    Jürgen Exner, Dec 13, 2007
    #2
    1. Advertising

  3. still just me

    Juha Laiho Guest

    "Jürgen Exner" <> said:
    >still just me wrote:
    >> I need to test if a file exists. Is there a better technique than
    >> "attempt open and watch for an error"? The program will be a cgi
    >> running in a windows or linux environment.

    >
    >Why not use the "file exists" operator? See
    >
    > perldoc -f -e


    Commenting on an old thread here, but it depends on what the OP wants to
    do with the file. Separate tests for file access permissions/file existence
    may create possibilities for race conditions (if there's a possibility
    that an existing file gets its permissions modified/gets removed
    between the test and actual desired operation).

    So, for most cases attempt to operate on the file in the desired way
    (and handling arising error conditions) is the best test.
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
    Juha Laiho, Dec 27, 2007
    #3
    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. Jim Hunter
    Replies:
    3
    Views:
    588
    Jim Hunter
    Mar 6, 2005
  2. Leon
    Replies:
    3
    Views:
    186
    Marco Mariani
    May 6, 2009
  3. Sven Wolf
    Replies:
    7
    Views:
    200
    Tad McClellan
    Dec 3, 2004
  4. pgodfrin
    Replies:
    6
    Views:
    88
    Ilya Zakharevich
    Jan 22, 2008
  5. Eric

    testing for existence of file

    Eric, May 16, 2006, in forum: Javascript
    Replies:
    4
    Views:
    98
    Thomas 'PointedEars' Lahn
    May 22, 2006
Loading...

Share This Page