| by Arround The Web | No comments

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:

readFile = open("readMe.txt", "r")

 

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

data = readFile.read()

 

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:

data = data.replace("World!", "Python!")

 

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:

writeFile = open("writeMe.txt", "w")
writeFile.write(data)

 

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 = readFile.read()

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:

from pathlib import Path

file = Path("readMe.txt")

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.

Share Button

Source: linuxhint.com

Leave a Reply