with
statement PTC-W0010with
statement to open a file29
30def write_to_file(results, save_file):
31 """Write everything stored in `results` to file at path `save_file`. Used to write results from `all_in_folder()` to `save_file`."""
32 file_results = open(save_file, "a+")33 logger.info("Writing results to file " + str(save_file))
34 for item in tqdm(
35 results, total=len(results), desc="> OCR: Writing To File Progress"
Opening a file using with
statement is preferred as function open
implements the context manager protocol that releases the resource when it is outside of the with
block. Not doing so requires you to manually release the resource.
f = open('/tmp/.deepsource.toml', 'w')
f.write("config file.")
# No `f.close()` statement: file may remain unaccessible
with open('/tmp/.deepsource.toml', 'w') as f:
f.write("config file.")