Python read file replace

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:

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

Читайте также:  While for foreach php

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:

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.

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()

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:

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.

[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:

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:

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!!

2 thoughts on “4 practical examples — Python string replace in file”

in example 3 , if I need to use one more condition (an elif instead of else) what can be the syntax. I am little confused with the indentation Reply

Источник

Python Replace String in File

Oftentimes, the data stored within files needs to be changed or replaced with some different data. This can be done in Python by using simple handling operations, which include opening, reading, writing, and closing the file using the built-in methods. This post will act as a guide on how to replace or change a string in the same file or in different files.

How to Replace String in Different Files?

If the user wants to read a file’s content, replace a certain string or a substring in the data and then store the new content in a separate file, then that is possible with the replace() method along with the open(), read() and write() methods. To demonstrate this, start by opening up the first file using the following line:

After that, read the content of the file by using the read() method:

The content of the file that we are reading is:

The goal is to replace “Hello World!” with “Hello Python!”, and to do this use the following replace() method:

Now the “data” variable contains the modified strings, all you need to do is open the output file (write mode) and write the new data inside it using the following lines:

After this code has been executed, open up the output file and observe the content:

As you can see in the output, a certain string has been replaced in the file’s content.

How to Replace String in the Same Files?

The same procedure that you have followed in the previous section can be used to change or replace a string’s content. To do this, use the following code:

readFile = open ( «readMe.txt» , «r» )

data = data.replace ( «World!» , «Python!» )

writeFile = open ( «readMe.txt» , «w» )
writeFile.write ( data )

In this code, the same file is opened first through the read mode, and then through the write mode, and when this code is executed, it replaces the content of the file:

The output verifies that the substring has been replaced in the file’s string

How to Replace String in File Using Path Package?

The Path package from the pathlib library is used to open a stream to a file with both read and write modes. This allows the user to simultaneously read the data from a file, replace its content and then write it back to the file. For this, the path module contains the function read_text() and write_text() respectively.

To perfect this replacement of string in a file’s string, take the following content of the file “readMe.txt”:

To replace the substring “some” with “replaced” use the following lines of code:

file.write_text ( file.read_text ( ) .replace ( «some» , «replaced» ) )

When this above code is executed, it produces the following changes in the file’s content:

It can be observed that the content of the file has been replaced according to the requirements.

Conclusion

To replace a string in a file the user can have two approaches, one is to change the content and place it in a different file and one is to place it in the same file. Both of these approaches can be performed with the help of the built-in open(), read(), replace(), and write() methods. Alternatively, to replace the content in the same file, the user can also utilize the Path module from the pathlib library.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor’s degree in computer science.

Источник

Оцените статью