Python: Inplace Editing using FileInput Last Updated : 19 Feb, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Python3 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: Example 2:Search and replace line with other line in file Text file: Python3 1== # 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: Example 3:Search text inline and replace that line with another line in the file. Text file: Python3 1== # 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: Example 4:Search text and replace that text in file. Text file: Python3 1== # 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: Comment More infoAdvertise with us Next Article fileinput.input() in Python S siddhantkumarupmanyu Follow Improve Article Tags : Python python-modules python-file-handling Practice Tags : python Similar Reads fileinput.lineno() in Python With the help of fileinput.lineno() method, we can get the line number for every line on line read from input file by using fileinput.lineno() method. Syntax : fileinput.lineno() Return : Return the line number. Example #1 : In this example we can see that by using fileinput.lineno() method, we are 1 min read Create an empty file using Python File handling is a very important concept for any programmer. It can be used for creating, deleting, and moving files, or to store application data, user configurations, videos, images, etc. Python too supports file handling and allows users to handle files i.e., to read and write files, along with 3 min read fileinput.filename() in Python With the help of fileinput.filename() method, we can get the last used file name which we have used so far by using fileinput.filename() method. Syntax : fileinput.filename() Return : Return the last used file name. Example #1 : In this example we can see that by using fileinput.filename() method, w 1 min read fileinput.input() in Python With the help of fileinput.input() method, we can get the file as input and can be used to update and append the data in the file by using fileinput.input() method. Python fileinput.input() Syntax Syntax : fileinput.input(files) Parameter : fileinput module in Python has input() for reading from mul 2 min read Python | Accepting Script Input A lot of people use Python as a replacement for shell scripts, using it to automate common system tasks, such as manipulating files, configuring systems, and so forth. This article aims to describe accepting Script Input via Redirection, Pipes, or Input Files. Problem - To have a script to be able t 2 min read fileinput.isfirstline() in Python With the help of fileinput.isfirstline() method, we can get the boolean value as True if line read from the file is very first line else false by using fileinput.isfirstline() method. Syntax : fileinput.isfirstline() Return : Return True if line read is first line of that file. Example #1 : In this 1 min read Like