Create a test file with an arbitrary size and random text content.
(path, size)
| 82 | fp.write(content) |
| 83 | |
| 84 | def write_test_file(path, size): |
| 85 | """Create a test file with an arbitrary size and random text content.""" |
| 86 | def chunks(total, step): |
| 87 | assert total >= step |
| 88 | while total > step: |
| 89 | yield step |
| 90 | total -= step |
| 91 | if total: |
| 92 | yield total |
| 93 | |
| 94 | bufsize = min(size, 8192) |
| 95 | chunk = b"".join([random.choice(string.ascii_letters).encode() |
| 96 | for i in range(bufsize)]) |
| 97 | with open(path, 'wb') as f: |
| 98 | for csize in chunks(size, bufsize): |
| 99 | f.write(chunk) |
| 100 | assert os.path.getsize(path) == size |
| 101 | |
| 102 | def read_file(path, binary=False): |
| 103 | """Return contents from a file located at *path*. |