This recipe shows how to create a simple version of the Unix split command in Python. The split command splits an input file into multiple smaller files, the size of each of which is specified by a command-line argument giving the number of lines per file. This is useful for multiple purposes, such as editing large files in pieces, backing up files to small capacity storage devices, sending files across the network, etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | import sys
import os
OUTFIL_PREFIX = "out_"
def make_out_filename(prefix, idx):
'''Make a filename with a serial number suffix.'''
return prefix + str(idx).zfill(4)
def split(in_filename, lines_per_file):
'''Split the input file in_filename into output files of
lines_per_file lines each. Last file may have less lines.'''
in_fil = open(in_filename, "r")
outfil_idx = 1
out_filename = make_out_filename(OUTFIL_PREFIX, outfil_idx)
out_fil = open(out_filename, "w")
# Using chain assignment feature of Python.
line_count = tot_line_count = file_count = 0
# Loop over the input and split it into multiple files.
# A text file is an iterable sequence, from Python 2.2,
# so the for line below works.
for lin in in_fil:
# Bump vars; change to next output file.
if line_count >= lines_per_file:
tot_line_count += line_count
line_count = 0
file_count += 1
out_fil.close()
outfil_idx += 1
out_filename = make_out_filename(OUTFIL_PREFIX, outfil_idx)
out_fil = open(out_filename, "w")
line_count += 1
out_fil.write(lin)
in_fil.close()
out_fil.close()
sys.stderr.write("Output is in file(s) with prefix {}\n".format(OUTFIL_PREFIX))
def usage():
sys.stderr.write(
"Usage: {} in_filename lines_per_file\n".format(sys.argv[0]))
def main():
if len(sys.argv) != 3:
usage()
sys.exit(1)
try:
# Get and validate in_filename.
in_filename = sys.argv[1]
# If input file does not exist, exit.
if not os.path.exists(in_filename):
sys.stderr.write("Error: Input file '{}' not found.\n".format(in_filename))
sys.exit(1)
# If input is empty, exit.
if os.path.getsize(in_filename) == 0:
sys.stderr.write("Error: Input file '{}' has no data.\n".format(in_filename))
sys.exit(1)
# Get and validate lines_per_file.
lines_per_file = int(sys.argv[2])
if lines_per_file <= 0:
sys.stderr.write("Error: lines_per_file cannot be less than or equal to 0.\n")
sys.exit(1)
# If all checks pass, split the file.
split(in_filename, lines_per_file)
except ValueError as ve:
sys.stderr.write("Caught ValueError: {}\n".format(repr(ve)))
except IOError as ioe:
sys.stderr.write("Caught IOError: {}\n".format(repr(ioe)))
except Exception as e:
sys.stderr.write("Caught Exception: {}\n".format(repr(e)))
raise
if __name__ == '__main__':
main()
|
The recipe does not implement the full feature set of the POSIX split command.
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/split.html
The POSIX / Unix split command creates output files with the (default) prefix x and suffixes such as aa, ab, etc. This recipe uses serial numbers starting from 1 as the suffix, and out_ as the prefix.
Basic splitting of a text file is supported, and specifying the number of lines per output file. Testing with different inputs and file sizes has been done to some extent.
More details and sample output in this blog post:
http://jugad2.blogspot.in/2016/03/unix-split-command-in-python.html