Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python: Inplace Editing using FileInput

  • Last Updated : 19 Feb, 2020

Python3’s fileinput provides many useful features that can be used to do many things without lots of code. It comes handy in many places but in this article, we’ll use the fileinput to do in-place editing in a text file. Basically we’ll be changing the text in a text file without creating any other file or overheads.

Syntax:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

FileInput(filename, inplace=True, backup='.bak')

Note: The backup is extension for the backup file created before editing.



Example 1:Changing only the first line of file

Text file:

fileinput-python-1




# Python code to change only first line of file
import fileinput
  
filename = "GFG.txt"
  
with fileinput.FileInput(filename, 
                         inplace = True, backup ='.bak') as f:
  
    for line in f:
        if f.isfirstline():
            print("changing only first line", end ='\n')
        else:
            print(line, end ='')

Output:

fileinput-python-2

Example 2:Search and replace line with other line in file

Text file:

fileinput-python-3






# python3 code to search and 
# replace line with other line in file
import fileinput
  
filename = "GFG.txt"
  
with fileinput.FileInput(filename,
                         inplace = True, backup ='.bak') as f:
      
    for line in f:
        if "search this line and change it\n" == line:
            print("changing the matched line with this line",
                  end ='\n')
        else:
            print(line, end ='')

Output:

fileinput-python-4

Example 3:Search text inline and replace that line with another line in the file.

Text file:

fileinput-python




# python3 code to search text in 
# line and replace that line with 
# other line in file
import fileinput
  
filename = "GFG.txt"
  
with fileinput.FileInput(filename,
                         inplace = True, backup ='.bak') as f:
    for line in f:
        if "searchtext" in line:
            print("changing this line with line that contains searched text",
                  end ='\n')
        else:
            print(line, end ='')

Output:

fileinput-python-6

Example 4:Search text and replace that text in file.

Text file:

fileinput-python1




# python code to search
# text and replace that text
# in file
  
import fileinput
  
filename = "GFG.txt"
  
with fileinput.FileInput(filename, 
                         inplace = True, backup ='.bak') as f:
      
    for line in f:
        if "replace text" in line:
            print(line.replace("replace text",
                               "changed text"), end ='')
        else:
            print(line, end ='')

Output:

fileinput-python-7




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!