Created
August 13, 2018 16:31
-
-
Save liunian/a367df2a42b89c5f4e597cf34e600b9d to your computer and use it in GitHub Desktop.
modify large randomly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import sys | |
import mmap | |
import string | |
import random | |
from pathlib import Path | |
def random_str(len): | |
return ''.join(random.choices(string.ascii_letters + string.digits + string.whitespace, k=len)) | |
def random_modify(file_path, count): | |
p = Path(file_path) | |
if(not os.path.exists(file_path)): | |
p.touch() | |
count = random.randint(1, count) | |
with p.open('a') as f: | |
size = os.path.getsize(file_path) | |
for i in range(count): | |
end = 0 if size == 0 else size - 1 | |
pos = random.randint(0, end) | |
f.seek(pos) | |
randstr_len = random.randint(1, 250) | |
s = random_str(randstr_len) | |
size += randstr_len | |
print(i, size, end, pos, '\n') | |
print("\n****\n") | |
f.write(s) | |
f.flush() | |
if __name__ == '__main__': | |
if (len(sys.argv) != 3): | |
print("Usage: python script.py file_path count") | |
exit(-1) | |
random_modify(sys.argv[1], int(sys.argv[2])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment