From e156683b30f70b54bea045aa0ada6a3c2b669517 Mon Sep 17 00:00:00 2001 From: gregorni Date: Thu, 15 Jun 2023 17:41:17 +0200 Subject: [PATCH] Write to file with FileType('r+') --- blueprintcompiler/main.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/blueprintcompiler/main.py b/blueprintcompiler/main.py index d1462ef..78121d9 100644 --- a/blueprintcompiler/main.py +++ b/blueprintcompiler/main.py @@ -76,9 +76,7 @@ class BlueprintApp: nargs="+", metavar="filenames", default=sys.stdin, - type=argparse.FileType( - "r" - ), # idk, but opening with "rw" somehow throws an error + type=argparse.FileType("r+"), ) port = self.add_subcommand("port", "Interactive porting tool", self.cmd_port) @@ -179,9 +177,9 @@ class BlueprintApp: if data != formatted: if not opts.check: - open(file.name, "w").write( - formatted - ) # This is very unelegant, should actually be `file.write(formatted)` + file.seek(0) + file.truncate() + file.write(formatted) formatted_files += 1