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
+2 -2
View File
@@ -42,7 +42,7 @@ def writeSourceToFile(lines):
# print("filePath is", filePath)
if filePath != False:
os.system("mkdir -p " + filePath)
f = open(srcName, mode='a+', encoding='utf8')
f = open(srcName, mode='a+', encoding='utf8', newline='')
createdSources.append(srcName)
i = 0
for idx, line in enumerate(lines[1:]):
@@ -63,7 +63,7 @@ if __name__ == '__main__':
try:
# decide if file has multiple sources
lines = open(filePath, mode='r', encoding='utf8').read().splitlines()
lines = open(filePath, mode='r', encoding='utf8', newline='').read().splitlines()
if lines[0][:12] == "==== Source:":
hasMultipleSources = True
writeSourceToFile(lines)