Should you always use with when printing to files in python -
from have seen, there 2 ways print file:
method 1
file = open('myfile.txt', 'a') file.write('sup') # later on file.write('yo') # @ end file.close()
method 2
with open('myfile.txt', 'a') f: f.write('sup') # later on open('myfile.txt', 'a') f: f.write('yo')
the problem first method, if program end abruptly, file not close , not saved. because of this, using , reopening file everytime want print it. however, realize may bad idea considering append file every 5 seconds. there major performance hit in reopening file "with" before printing every time? , if so, how should handle abrupt endings result in file not being closed using first method.
in first method, want flush changes file system after done set of writes. i.e.:
file.flush()
for simple had in example, yes should use with open...
interacting files.
Comments
Post a Comment