Read/write files in python with newline='' option to preserve newlines as \n on Windows

This commit is contained in:
Kamil Śliwak
2021-01-22 12:16:27 +01:00
parent cc516b2a16
commit 151df00bb0
10 changed files with 101 additions and 9 deletions
+3 -1
View File
@@ -42,7 +42,9 @@ class FileReport:
def load_source(path: Union[Path, str]) -> str:
with open(path, mode='r', encoding='utf8') as source_file:
# NOTE: newline='' disables newline conversion.
# We want the file exactly as is because changing even a single byte in the source affects metadata.
with open(path, mode='r', encoding='utf8', newline='') as source_file:
file_content = source_file.read()
return file_content