diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-07-19 13:21:08 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-07-19 13:21:08 +0300 |
commit | bf92322f7eafdae5b7ae5a8da4486a277d01307d (patch) | |
tree | cc33ffc039c999ef5c15d24b54a13391477a38db /subplot/data.py | |
parent | 891e1155495e5c42beafa6cfd9b518c804e9978c (diff) | |
download | obnam2-bf92322f7eafdae5b7ae5a8da4486a277d01307d.tar.gz |
test: use better way to write files in Python
"open(...).write(...)" does not necessarily close the file, and thus
flush buffered writes to disk. "with open(...)" does.
Sponsored-by: author
Diffstat (limited to 'subplot/data.py')
-rw-r--r-- | subplot/data.py | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/subplot/data.py b/subplot/data.py index 2e2802e..583e52d 100644 --- a/subplot/data.py +++ b/subplot/data.py @@ -11,7 +11,8 @@ def create_file_with_given_data(ctx, filename=None, data=None): logging.debug(f"creating file {filename} with {data!r}") dirname = os.path.dirname(filename) or "." os.makedirs(dirname, exist_ok=True) - open(filename, "wb").write(data.encode("UTF-8")) + with open(filename, "wb") as f: + f.write(data.encode("UTF-8")) def create_file_with_random_data(ctx, filename=None): @@ -33,7 +34,8 @@ def create_cachedir_tag_in(ctx, dirpath=None): filepath = f"{dirpath}/CACHEDIR.TAG" logging.debug(f"creating {filepath}") os.makedirs(dirpath, exist_ok=True) - open(filepath, "w").write("Signature: 8a477f597d28d172789f06886806bc55") + with open(filepath, "w") as f: + f.write("Signature: 8a477f597d28d172789f06886806bc55") def create_nonutf8_filename(ctx, dirname=None): @@ -69,7 +71,8 @@ def _create_manifest_of_directory(ctx, dirname=None, manifest=None): runcmd_run(ctx, ["find", "-exec", "summain", "{}", "+"], cwd=dirname) assert runcmd_get_exit_code(ctx) == 0 stdout = runcmd_get_stdout(ctx) - open(manifest, "w").write(stdout) + with open(manifest, "w") as f: + f.write(stdout) def file_is_restored(ctx, filename=None, restored=None): |