Reputation: 3860
code :
f = open('workfile', 'r') Line #1
if f != None : Line #2
print f.read() Line #3
workfile is not there in the directory, so understandably python create one.(Although in python documentation they did not mention it ). ok
but when i change the file name from 'workfile to 'names 'with same code
f = open('names', 'r') Line #1
it shows IOError : No such file or directory.
why didn't it created another file of 'names' ? as it did in case of 'workfile'
Upvotes: 0
Views: 404
Reputation: 10489
You are trying to open the file for reading using the r
argument.
For python to try and create a file you need to specify the write command w
like:
f = open('names', 'w')
or if you want to append to an already created file you use the a
command:
f = open('names', 'a')
or for both reading and writing (will create a file):
f = open('names', 'r+')
The python documentation is pretty good to read up on too if you have queries.
Upvotes: 2