moderators - please delete
Errors when creating and writing to text file
Collapse
X
-
Errors when creating and writing to text file
Tags: None -
Originally posted by eddiefisher41Hello.
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
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