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

    Join Date
    Oct 2003
    Posts
    146
    Rep Power
    12

    Checking for an existing file


    Hi all, I am new to python and an carrying out a bit of work in which I must retrieve emails and copy its Body of text into a text file. But before I can write this text to a file I need to check whether the file I am writing to already exists or not. Is there a specific function I can use to do this?

    I've done some searching online and can't really find anything useful, apart from a mention of the Class 'path'???

    Cheers peps

    Tryst
    Tryst
  2. #2
  3. onCsdfeu
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Canada
    Posts
    100
    Rep Power
    12
    The os.path module has a isfile function which'll return true if the file name you pass as an argument exits.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    146
    Rep Power
    12
    Hi, I have tested some code with the information you have given me, and am coming up with an error (error states that the name/function isfile does not exist).

    This is the simple piece of code...

    import os.path

    filename = "test HTML.txt"
    if(isfile(filename)):
    print "It exists!!"
    else:
    print "It does not exist!"

    Why isn't it recognising the function isfile?

    Cheers
    Tryst
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    146
    Rep Power
    12
    I've solved it!

    it is...

    os.path.isfile(...filename/path...)

    Cheers for the help Solarbear

    Tryst
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You could have also imported the functions using the "form module import function" form and you're first example would have worked fine ...

    Code:
    from os.path import *
    
    path = "test HTML.txt"
    if(isfile(path)):
        print "It exists!!"
    else:
        print "It does not exist!"
    Also you dont have to import os.path directly, although its perfectly a valid way to get os.path, i just find it much nicer to import os which makes os.path available .

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    146
    Rep Power
    12
    You learn something new everyday :-)

    Cheers guys

    Tryst
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    14
    Do not use from <module> import *. That is all.
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Comon . There can be nothing that wrong with using import * as long as you know what you're getting. Although i myself like to see where i'm calling functions from. Unless you have somthing i havnt considered to add?

    Mark
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo