4 practical examples - Python string replace in file

In this tutorial I will share different examples to find and replace (string replace) in file using Python programming language. In our previous articles we discussed about python reading and writing to a file but here we will focus on writing to the same file i.e. modifying the same file using Python.

I am using Python 3.6 for all the examples:

Advertisement
~]# python3 -V
Python 3.6.8

 

Example-1: Python string replace in different file

In this example we have an input file with following content:

~]# cat a.txt
line 1
line 2
this is an input file
line 4
line 5

Here we wish to replace "input" with the word "output" and then save the entire content in a different file b.txt. Following is my script to perform this operation:

 ~]# cat example-1.py
#!/usr/bin/env python3

f1 = open('a.txt', 'r')
f2 = open('b.txt', 'w')

for line in f1:
    f2.write(line.replace('input', 'output'))
f1.close()
f2.close()

Here, we open both the files and store the content in a file object. The input file i.e. a.txt is opened in read only mode while the output file i.e. b.txt is opened in write mode. You can learn more about How to perform read write operation in files using Python programming

Next we perform the replace operation and write the output in f2 file object which is used to write in b.txt. Lets' execute our script:

 ~]# python3 example-1.py

As we are not printing anything on the console, the STDOUT is blank. Verify the content of b.txt:

4 practical examples - Python string replace in file

So the script has successfully replaced all instances of input with output and saved the output in a different file.

Advertisement

 

Example-2: Python string replace in same file

In this example we will perform the string replace operation in the same file. The difference would be that in this case we store the data of the file in the memory, perform the replacement and then overwrite the content of the input file with the data we stored in the memory. So similar to previous example, we will replace all instances of "input" with "output" string in the same file.

This method is safe to use unless you have got a huge file to work with which is too big to load into memory in one go, or you are concerned about potential data loss if the process is interrupted during the second step in which you write data to the file.

Here is the content of my input file a.txt:

4 practical examples - Python string replace in file

Following is our python code:

~]# cat example-2.py
#!/usr/bin/env python3

# Read the content of file
f1 = open('a.txt', 'r')
input_data = f1.read()
f1.close()

# Replace the target string
input_data = input_data.replace('input', 'output')

# Write the output to same file
f2 = open('a.txt', 'w')
f2.write(input_data)
f2.close()

Let's execute the script:

~]# python3 example-2.py

Since we are not printing anything on the console, the output is blank. Let's verify the content of our input file:

4 practical examples - Python string replace in file

As expected, all the occurrence of "input" is changed to "output" within the same file.

 

Example-3: Python find and replace text in the same file

In this example I have a file /tmp/input.txt with the following content:

Advertisement

4 practical examples - Python string replace in file

Here I wish to replace all the occurrences of initrd.img with patch.img. For this purpose we will use fileinput module.

Syntax to be used with fileinput module:

import fileinput
for line in fileinput.input():
    process(line)

We will open the file and store it in a file object, later using for loop we will iterate over each line and then perform find and replace operation. Following is my sample code:

#!/usr/bin/env python3

import fileinput

input_file = "/tmp/input.txt"

file_object = open( input_file, 'r+' )
for line in fileinput.input(input_file):
    if 'initrd.img' in line:
        print('Match')
    else:
        print('No Match')
    file_object.write(line.replace('initrd.img', 'patch.img'))

file_object.close()

I have added an extra if condition to print Match and No Match which is completely optional, I have just added it have some debug output. The r+ mode opens a file for both reading and writing. The file pointer placed at the beginning of the file.

Output from the script:

[root@centos8 ~]# python3 example-1.py
No Match
No Match
No Match
No Match
No Match
Match
No Match
No Match

So there was a single match, and here is our input.txt after executing the script:

4 practical examples - Python string replace in file

 

Example-4: Using fileinput module

In the previous example also we used fileinput module but we also were relying on inbuilt python function to open the file in read write mode and then performing the operation. In this example we will only use fileinput module to find and replace string in the same file.

Following is our input file:

Advertisement

4 practical examples - Python string replace in file

Here we will replace all instances of 'input' with 'output' in the same file.

~]# cat example-4.py
#!/usr/bin/env python3
import fileinput

input_file = "input.txt"
with fileinput.FileInput(input_file, inplace=True, backup='.bak') as file_object:
    for line in file_object:
        print(line.replace('input', 'output'), end='')

file_object.close()

Let's us execute the script:

~]# python3 example-4.py

The execution will update the content of input.txt and since we had added backup='.bak, so a new backup file will also be created.

4 practical examples - Python string replace in file

 

Conclusion

In this tutorial we covered different examples to perform find replace action and string replace in the same file or a different file using Python programming. You can choose the example as per your requirement as each have their own pros and cons. For example if you have a very big file where you have to perform the operation then it is better to avoid example-2 where we stored the content in the memory.

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment