I have a large text file and I want to split it into a few different smaller text files. Maybe someone has code for that?
Original file:
111
222
333
444
555
666
then split it to 3 txt files
File 1
111
222
File 2
333
444
File 3
555
666
I have a large text file and I want to split it into a few different smaller text files. Maybe someone has code for that?
Original file:
111
222
333
444
555
666
then split it to 3 txt files
File 1
111
222
File 2
333
444
File 3
555
666
If you want to split your original files into 3 files, without splitting lines, and getting the pieces into file_01, file_02 and file_03, try this:
split --numeric-suffixes=1 -n l/3 original_file file_
With GNU awk:
awk 'NR%2!=0{print >"File " ++c}; NR%2==0{print >"File " c}' original_file
or shorter:
awk 'NR%2!=0{++c} {print >"File " c}' file
%
is modulo operation
edit: Question originally asked for a pythonic solution.
There are similar questions throughout the site, but here's a solution to your example:
# read ('r') the file ('text.txt'), and split at each line break ('\n')
textFile = open('text.txt','r').read().split('\n')
# setup temporary array as a place holder for the files (stored as strings) to write,
# and a counter (i) as a pointer
temp = ['']
i = 0
# for each index and element in textfile
for ind,element in enumerate(textFile):
# add the element to the placeholder
temp[i] += element+'\n'
# if the index is odd, and we are not at the end of the text file,
# create a new string for the next file
if ind%2 and ind<len(textFile)-1:
temp.append('')
i += 1
# go through each index and string of the temporary array
for ind,string in enumerate(temp):
# write as a .txt file, named 'output'+the index of the array (output0, output1, etc.
with open('output'+str(ind)+'.txt','w') as output:
output.write(string)