#1
  1. No Profile Picture
    404 Error
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    http://hester.dns2go.com
    Posts
    18
    Rep Power
    0

    how to see if file exists?


    how can i see if a file exists? I have tried using the os._exists function but it doesnt work.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69

    os.path


    I've never heard of the _exists() function before, i'll have to look into it. Anyway I'd use the os.path's exists() function..

    >>> import os
    >>> os.path.exists('c:\\windows\\desktop\\somefile.txt')
    True
    >>>

    This will return True if the path exists (whether the path points at a file or a dir'), if you want to make sure that the path is a file you could use the isfile() function

    >>> import os
    >>> os.path.exists('c:\\windows\\desktop\\somefile.txt') and os.path.isfile('c:\\windows\\desktop\\somefile.txt')
    True
    >>>

    You can read more about the os.path module in the python docs (http://www.python.org/doc/current/li...e-os.path.html)

    Note: You can also do "import os.path"

    Mark.
    Last edited by netytan; September 14th, 2003 at 09:36 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    35
    Rep Power
    11
    I'm pretty sure that if isfile(...) returns true, then the file must exist. In which case, you can reduce the expression

    Code:
    os.path.exists('c:\\windows\\desktop\\somefile.txt') and os.path.isfile('c:\\windows\\desktop\\somefile.txt')
    to simply

    Code:
    os.path.isfile('c:\\windows\\desktop\\somefile.txt')
    Just my two cents,

    theperfectsoup
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    lol sorry, must not have been thinking that day .. for some reason i had it in my head that isfile retuned true if the path was a file, false if it was another kind of object (dir, link, mount etc.) and an error if the path didn't exist (because if couldn't be tested i guess). Don't ask me why , thanks for the info soup

    Mark.
    Last edited by netytan; September 18th, 2003 at 05:46 PM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2007
    Posts
    2
    Rep Power
    0

    Could try this also!


    I dont know how useful this could be, but here is a small script I wrote, that:
    1. Checks if a filename to work upon exists.
    2. Opens the file by that name if the file exists
    3. Else simply acts on (reproduces) itself word by word.


    import sys

    try:
    fh=file(sys.argv[1])
    except IndexError:
    fh=file(sys.argv[0])

    for line in fh.readlines():
    line=line.split(" ")
    for word in line:
    print word,


    This script also checks for the existence of file with given filename, and hence wanted to provide it here! Just hope that someone might find it useful!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2007
    Posts
    2
    Rep Power
    0

    Exclamation


    Kindly read the above script with the following indents.

    import sys

    try:
    fh=file(sys.argv[1])
    except IndexError:
    fh=file(sys.argv[0])

    for line in fh.readlines():
    line=line.split(" ")
    for word in line:
    print word,

IMN logo majestic logo threadwatch logo seochat tools logo