I am struggling with splitting a text file into two.
For example this splits the file into two but if there is an uneven number of lines it creates a third file:
for line in infile:
count_lines += 1
lines_per_file = int(count_lines / 2)
subprocess.call(['split', '-l', str(lines_per_file), '--numeric-suffixes', infile, chunk_destination])
While this splits the file into two but cuts lines in half:
subprocess.call(['split', '-n', '1/2', '--numeric-suffixes', infile, chunk_destination])
Is there a relatively simple way of splitting a file into two with Python or Bash that will add the extra line (if number of lines is uneven) into one of the two existing files instead of making a third or splitting into two files but preserving lines?
Actually newer version of split has an option to preserve lines:
CHUNKS may be: N split into N files based on size of input K/N output Kth of N to stdout l/N split into N files without splitting lines/records l/K/N output Kth of N to stdout without splitting lines/records r/N like 'l' but use round robin distribution r/K/N likewise but only output Kth of N to stdout
so this works:
subprocess.call(['split', '-n', 'l/2', '--numeric-suffixes', infile, chunk_destination])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.