Search code examples
pythonpython-3.xfiletextio

How to open txt file and put content in tsv file in python


I'm new to python, and have a very basic understanding of file io.

I want to open a text file, change the formatting of the content inside it and place that changed content into a new tsv file.

I know how to open the file with

old_file = open('old_file.txt')

but have no idea how to read it line by line without changing the original text file, or how to make a new file for the changes. Any help would be greatly appreciated.


Solution

  • Try this :

    fp = open('new_file.txt','w') 
    for line in open('old_file.txt','r').readlines():
       new_line = 'do modifications on line and store to new line'
       fp.write(new_line)
    fp.close()