Overwrite file in Python

Python overwrite file

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,

Contents of the file:

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.

Contents of the file:

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,

Contents of the file.

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.

Contents of the file.

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,

Contents of the file.

That’s all about how to overwrite file in Python.

Related Posts

  • Iterate through files in directory in Python
    30 August

    Iterate through files in directory in python

    Table of ContentsUsing the os.scandir() function to iterate through files in a given directory in Python.Using the os.listdir() function to iterate through files in a given directory in Python.Using the os.walk() function to iterate through files in a given directory in Python.Using the pathlib module to iterate through files in a given directory in Python.Using […]

  • Write a list to a file in Python
    12 June

    Write a list to a file in python

    Table of ContentsUsing the write() function to write a list to a file in PythonUsing the writelines() function to write a list to a file in PythonUsing the pickle.dump() function to write a list to a file in PythonUsing the json.dump() function to write a list to a file in PythonUsing the unpack operator * […]

  • Get filename from path in Python
    21 May

    Get filename from Path in Python

    Table of ContentsGet filename from Path in Python using the ntpath ModuleWindowsLinuxGet filename from Path in Python using the os.path.basename() FunctionGet filename from Path in Python using the os.path.split() FunctionGet filename from Path in Python using the pathlib.path().name MethodConclusion There are different ways to define the path of the file. In Windows, the path separator […]

  • Python read text file line by line
    13 May

    Read text file line by line in Python

    Learn about how to read text file line by line in Python.

  • 26 March

    Python list files in directory

    Table of ContentsUsing os.walkUsing os.listdir(path)Using globConclusion In this post, we will see how to list all files in a directory in Python. There are multiple ways to list all files in a directory in Python. Using os.walk Python os module provides multiple function to get list of files. List all files in directory and its […]

  • 23 March

    Python write to text file

    Table of ContentsCreate txt file(‘x’)Write to txt file(‘w’)Append to txt file(‘a’) In this post, we will see how to write to a text file. There are multiple modes in which you can write to text file. Create txt file(‘x’) It will open the file in 'X' mode which will create new file and return error […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.