I need to write a code where I have huge 7000+ lines of text file. I need to split every 10lines and write it to another file.
-
2Possible duplicate - https://stackoverflow.com/a/41937429/4098013 – Vivek Kalyanarangan Nov 16 '17 at 05:14
-
1Why write a python script to do that? Look at https://linux.die.net/man/1/split – Red Cricket Nov 16 '17 at 05:15
2 Answers
Open the files, then iterate through the input lines writing every 10th line to the output file:
with open(in_name, 'r') as f:
with open(out_name, 'w') as g:
count = 0
for line in f:
if count % 10 == 0:
g.write(line)
count += 1
The open()
context manager will close the files when the scope is exited.
Since the decision to output is simply counting you could use a slice f.readlines()[::10]
although if the file is big the itertools islice generator might be more apropriate.
from itertools import islice
with open(in_name, 'r') as f:
with open(out_name, 'w') as g:
g.writelines( islice(f, 0, None, 10) ):
I read your question as wanting to write every 10th line. If you want to write lots of files containing 10 chunks of the file you'll need to loop until the input file is exhausted. This is not the same as the question that is shown as a duplicate. That answer breaks if the chunking reads past the end of the file.
from itertools import islice, count
out_name = 'chunk_{}.txt'
with open(in_name) as f:
for c in count():
chunk = list(islice(f, 10))
if not chunk:
break
with open(out_name.format(c)) as g:
g.writelines(chunk)

- 1,733
- 10
- 14
with open(fname) as f:
content = f.readlines()
with open(fname) as g:
len_f = len(content)
for x in xrange(0, len_f):
if x % 10 = 0:
g.write(content[x])
g.write("\n") #For new-line
else:
pass
g.close()
f.close()
Should work! (Python 2.x)
Key take-aways:
1) Don't open / close the write-file after writing each line.
2) Close the files upon completion.

- 447
- 1
- 5
- 17