Skip to content

Instantly share code, notes, and snippets.

@lewisxy
Created December 3, 2019 22:04
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 lewisxy/864ba77cdc668360ded04a8c5b05aecd to your computer and use it in GitHub Desktop.
Save lewisxy/864ba77cdc668360ded04a8c5b05aecd to your computer and use it in GitHub Desktop.
Split a file into smaller pieces
#!/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