Skip to content

Commit 60d5254

Browse files
committed
write_file function improvement
1 parent 1e33b43 commit 60d5254

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

flaskcode/utils.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,18 @@ def get_file_extension(filename):
1414
return ext.lower()
1515

1616

17-
def write_file(content, filepath, encoding='utf-8', chunk_size=None):
17+
def write_file(content, filepath, encoding='utf-8', newline='\n', chunk_size=None):
1818
success = True
1919
message = 'File saved successfully'
2020
if isinstance(content, str):
21-
content_io = io.StringIO()
22-
content_io.write(content)
23-
with io.open(filepath, 'w', encoding=encoding, newline='\n') as dest:
24-
content_io.seek(0)
21+
content_buffer = io.StringIO(content, newline=newline)
22+
with io.open(filepath, 'w', encoding=encoding, newline=newline) as dest:
23+
content_buffer.seek(0)
2524
try:
26-
shutil.copyfileobj(content_io, dest, chunk_size or DEFAULT_CHUNK_SIZE)
27-
except OSError as e:
25+
shutil.copyfileobj(content_buffer, dest, chunk_size or DEFAULT_CHUNK_SIZE)
26+
except OSError as err:
2827
success = False
29-
message = 'Could not save file: ' + str(e)
28+
message = 'Could not save file: ' + str(err)
3029
else:
3130
success = False
3231
message = 'Could not save file: Invalid content'

0 commit comments

Comments
 (0)