Reputation: 1
I have a text file with 600 lines(\n), and I want to make 600 text files saving each line. So I want to write text files line by line, but I don't know how I can make it.
e.g. If I have 'all.txt' file with 600 lines, like: line1 aaaaaa line2 bbbbbb line3 cccccc ... line 600 zzzzzz
-> I want to make results like this: 1.txt (content: aaaaaa) 2.txt (content: bbbbbb) 3.txt (content: cccccc) ... 600.txt (content: zzzzzz)
I have python and notepad++, so it would be really nice to make these results using them. Thank you so much :)
This is what I tried, but the result was just 'i.txt' file with the last 600th line saved.
with open('all.txt',encoding='utf-8') as f:
content = f.readlines()
len(content)
600
for i, line in enumerate(content):
with open('i.txt', 'w') as f:
f.write(content[i])
Upvotes: -1
Views: 782
Reputation: 89507
with open('all.txt') as f:
for i, line in enumerate(f, 1):
with open(f'{i}.txt', 'w') as f2:
f2.write(line)
Upvotes: 1