I have a text file containing one string per line and I want to split each line of this text file and that each line should be stored as a separate text file.
Could you please help me out with this?
I have a text file containing one string per line and I want to split each line of this text file and that each line should be stored as a separate text file.
Could you please help me out with this?
with open('filename.txt') as file:
lines = file.readlines()
# use an identifier from each line to ensure new file name is unique
line_num = 0
for line in lines:
try:
with open('line_{0}.txt'.format(line_num), 'w') as new_file:
new_file.write(line)
# increment the identifier
line_num+=1
except ():
print('Error occured')