#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Verify if file exist


    I am new to this forum and I am new to python programming.

    I have to write a python program which :
    - read the path and the file to verify from a parameter. Input file to be verified could be on an external drive, a USB key, a SDHC card, a SAN (storage Area Network).
    - have to verify if a given files exist or not
    - if the file does not exist the program is to finish with an error number equal -99. This error is to be trapped by another program. For example, in applescript I would like to do on error.

    At this moment, I've written the following line of code. For some reason the program says that the file does not exist when it does. I am not getting the path and the filename being passed by a parameter. I am keeping it simple.

    I am using a MacBook Pro for writing the program.

    from os import path
    PATH = "Macintosh HD/IMG_3859.JPG"
    if path.exists(PATH) and path.isfile(PATH):
    print "File does exist"
    else:
    print "File doesn't exist!", PATH

    Thanks for your help!
    Daniel
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    This will put you a little closer to the end product:
    Code:
    import sys
    from os import path
    
    print(sys.argv) # The command line arguments are here
    
    PATH = "Macintosh HD/IMG_3859.JPG"  # try an absolute path
    
    if path.exists(PATH) and path.isfile(PATH):
        print "File does exist"
    else:
        print "File doesn't exist!", PATH
        sys.exit(-99)
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo