1

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
hornetbzz
  • 9,188
  • 5
  • 36
  • 53
November
  • 63
  • 6
  • This isn't a python suggestion but if you are working on the linux/mac command line, you can use the `split` function. Are you specifically looking for a python solution ? – steveb Feb 07 '18 at 16:11
  • in your case, maybe command `split` is working: `split --lines=100 filename` – Haifeng Zhang Feb 07 '18 at 16:12
  • Possible duplicate of [How can I split a file in python?](https://stackoverflow.com/questions/546508/how-can-i-split-a-file-in-python) – Arount Feb 07 '18 at 16:36
  • Thanks, checked before, but somehow missed! – November Feb 07 '18 at 16:51

3 Answers3

1

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_
Pierre François
  • 5,850
  • 1
  • 17
  • 38
1

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

Cyrus
  • 84,225
  • 14
  • 89
  • 153
0

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)