Python file open/close every time vs keeping it open until the process is finished
You should definitely try to open/close the file as little as possible
Because even comparing with file read/write, file open/close is far more expensive
Consider two code blocks:
f=open('test1.txt', 'w')
for i in range(1000):
f.write('\n')
f.close()
and
for i in range(1000):
f=open('test2.txt', 'a')
f.write('\n')
f.close()
The first one takes 0.025s while the second one takes 0.309s
Use the with
statement, it automatically closes the files for you, do all the operations inside the with
block, so it'll keep the files open for you and will close the files once you're out of the with
block.
with open(inputfile)as f1, open('dog.txt','a') as f2,open('cat.txt') as f3:
#do something here
EDIT:
If you know all the possible filenames to be used before the compilation of your code then using with
is a better option and if you don't then you should use your approach but instead of closing the file you can flush
the data to the file using writefile1.flush()