-1

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?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61

2 Answers2

0

This would work:

with open('filename') as file:
    for line in file:
        with open("change file name every time in this loop", "x") as newFile:
            newFile.write(line)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Jesper
  • 1,007
  • 7
  • 24
0
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')