I am trying to read each line of a txt
file and print out each line in a different file. Suppose, I have a file with text like this:
How are you? I am good.
Wow, that's great.
This is a text file.
......
Now, I want filename1.txt
to have the following content:
How are you? I am good.
filename2.txt
to have:
Wow, that's great.
and so on.
My code is:
#! /usr/bin/Python
for i in range(1,4): // this range should increase with number of lines
with open('testdata.txt', 'r') as input:
with open('filename%i.txt' %i, 'w') as output:
for line in input:
output.write(line)
What I am getting is, all the files are having all the lines of the file. I want each file to have only 1 line, as explained above.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…