Skip to content

Instantly share code, notes, and snippets.

@liunian
Created August 13, 2018 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liunian/a367df2a42b89c5f4e597cf34e600b9d to your computer and use it in GitHub Desktop.
Save liunian/a367df2a42b89c5f4e597cf34e600b9d to your computer and use it in GitHub Desktop.
modify large randomly
#!/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