Errors when creating and writing to text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eddiefisher41
    New Member
    • Jun 2007
    • 15

    Errors when creating and writing to text file

    moderators - please delete
    Last edited by eddiefisher41; Aug 22 '08, 04:17 PM. Reason: no longer needed
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by eddiefisher41
    Hello.
    Im coming back to python after quite a while.
    Im wanting to create a text file that i can later write information to.
    If i run this segment of code:

    global_file = open('sample_si ze.txt', 'r+')

    It should create a new file called sample_size.txt in the current directory, probably C: and return a handler to the newly created and open file.
    When i run this i get:

    IOError: [Errno 2] No such file or directory: 'sample_size.tx t'

    Why am i getting this when the open function should create the file?

    In a different function i call the exists function to see if this file is present in the current directory, if its not there then i create it using the above code.

    os.path.exists( '.\sample_size. txt')
    Returns false but no error even if i manually create the file. I can see it so why does this function return false. I tried using the exists function with the full path of the file only to get the same result???

    Thanks.
    Ed Fisher
    Ed,

    Use 'a' or 'w' mode to create a file if it does not exist. Mode 'w' truncates an existing file, and 'a' appends to an existing file.

    BV

    Comment

    Working...