Table of Contents
File Handling in Python
We can read and write content to files in Python. We use the open()
function to create files and specify the path and mode for the required file. This creates a file handling object that handles different file operations.
Ways to overwrite file in Python
To overwrite a file, we need to delete its content. In this article, we will overwrite a file in Python.
Using the w
parameter in the open()
function
We can open the files in different modes. We have the write (w
), read (r
), and append (a
) mode. When we open the file in the write mode, it removes all the previous contents of the file, and we can write data to this file.
For example,
1 2 3 4 |
with open('file.txt','w') as f: f.write("New Content") |
Contents of the file:
1 2 3 |
New Content |
Using the file.truncate()
function
The truncate()
function allows us to remove the contents of the file. For this, we need to ensure that the file pointer is at the start of the file. To achieve this, we use the file.seek()
function and set the pointer to the start.
This method is suitable when we open the file in the read mode.
See the code below.
1 2 3 4 5 6 |
with open('file.txt','r+') as f: f.seek(0) f.write("New Content") f.truncate() |
Contents of the file:
1 2 3 |
New Content |
In the above example,
- The
r+
mode opens the file in reading and writing mode. - The
f.seek(0)
puts the file pointer to the start of the file.
Using the replace()
function
The replace()
function allows us to replace a given string with a different string. With this function, we can overwrite an existing file by replacing a specific phrase.
For example,
1 2 3 4 5 6 7 8 9 |
f1 = open('myfile.txt','r') data = f1.read() data = data.replace('abc','def') f1.close() f2 = open('myfile.txt','w') f2.write(data) f2.close() |
Contents of the file.
1 2 3 |
replaced string def |
In the above code,
- We first open the file in the read mode.
- We read the contents of the file.
- Then, we replace the required phrase from the contents using the
replace()
function. - We write this new data to the same file by opening it in the write mode, automatically removing its previous content.
Using the pathlib
module
For users working with Python 3, the pathlib
module can access files and read and write the contents. We can overwrite the contents and replace the contents of the file using the write_text()
function and the re.sub()
function. The re.sub()
function replaces a string given it matches a regular expression pattern or not.
See the code below.
1 2 3 4 5 6 7 |
import re from pathlib import Path filepath = Path("myfile.txt") data = filepath.read_text() filepath.write_text(re.sub(r"def",r"abc", data)) |
Contents of the file.
1 2 3 |
replaced string abc |
Let us understand what is happening in the above example.
- We read the given file using its path and specifying it in the
Path()
function. - The
read_text()
stores its content in a variable. - We replace the string using the
re.sub()
function. - The
write_text()
writes the new file’s contents and removes its previous data.
Using the os.remove()
function
If we wish to overwrite file, we can delete it and create a new file. To delete a file, we can use the os.remove()
function.
We will first check whether a given file exists or not using the os.path.exists()
function. If the file exists, we remove it using the os.remove()
function.
After this, we create a new file of the same name and write data to it.
For example,
1 2 3 4 5 6 7 8 9 10 |
import os if (os.path.exists("myfile.txt")): os.remove("myfile.txt") else: print("File Does Not Exists") f = open("myfile.txt", "w") f.write('ABC') f.close() |
Contents of the file.
1 2 3 |
ABC |
That’s all about how to overwrite file in Python.