Created
December 3, 2019 22:04
-
-
Save lewisxy/864ba77cdc668360ded04a8c5b05aecd to your computer and use it in GitHub Desktop.
Split a file into smaller pieces
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
#!/bin/python3 | |
import sys | |
# This program splits a file into smaller files of given size | |
if len(sys.argv) == 3: | |
fname = sys.argv[1] | |
osize = int(sys.argv[2]) | |
try: | |
with open(fname, 'rb') as f: | |
f.seek(0, 2) | |
fsize = f.tell() | |
print(fsize) | |
f.seek(0) | |
count = 0 | |
while (count * osize < fsize): | |
with open(fname + '.' + str(count), 'wb') as g: | |
g.write(f.read(min(osize, fsize - count * osize))) | |
count += 1 | |
except Exception as e: | |
print(e) | |
else: | |
print("usage: {} <filename> <output_size>".format(sys.argv[0])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment