Python: Inplace Editing using FileInput

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:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 ='')

chevron_right


Output:

fileinput-python-2

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

Text file:

fileinput-python-3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 ='')

chevron_right


Output:

fileinput-python-4



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

Text file:

fileinput-python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 ='')

chevron_right


Output:

fileinput-python-6

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

Text file:

fileinput-python1

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 ='')

chevron_right


Output:

fileinput-python-7




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.