Skip to content

Commit

Permalink
Write to the file in chunks to avoid MemoryError
Browse files Browse the repository at this point in the history
  • Loading branch information
jesusjimsa committed Nov 15, 2020
1 parent ece0530 commit 09b277b
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# This program is free software; you can redistribute it and/or modify it under the terms of GPLv2

import os
import random
import string

import pytest

Expand Down Expand Up @@ -50,14 +52,17 @@ def create_and_restore_large_file(request):
if not os.path.exists(testdir1):
os.mkdir(testdir1)

file_size = 1024 * 1024 * 768 # 805 MB
chunksize = 1024 * 768
file_path = os.path.join(testdir1, 'large_file')
changed_path = os.path.join(testdir1, 'changed_name')

if os.path.exists(changed_path):
os.rename(changed_path, file_path)
elif not os.path.exists(file_path):
with open(file_path, "a") as f:
f.write('a' * 1024 * 1024 * 768) # 805 MB
while os.stat(file_path).st_size < file_size:
f.write(random.choice(string.printable) * chunksize)

# Tests

Expand Down

0 comments on commit 09b277b

Please sign in to comment.