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.

Was this post helpful?

Related Posts

  • 19 May

    Remove Extension From Filename in Python

    Table of ContentsHow to Remove Extension From Filename in Python?Remove Extension From Filename in Python Using the os ModuleRemove Extension From Filename in Python using the pathlib moduleRemove Multiple File Extensions Using pathlib Module in PythonConclusionWas this post helpful? While programming in python, we often have to deal with file names. In this article, we […]

  • 19 May

    Get Directory Name From File Path in Python

    Table of ContentsHow to Get directory name from file path in python?Get Directory Name From File Path Using the os Module in PythonGet Directory Name From File Path Using the pathlib ModuleRemove FilnName from File PathRemove Filename From the File Path Using the OS Module in PythonRemove Filename From File Path Using the Pathlib Module […]

  • 19 May

    Print Current Directory in Python

    Table of ContentsFiles and Directories in PythonWays to print the Current Directory in PythonUsing the os.getcwd() function to print the current directory in PythonUsing the os.path module to print the current directory in PythonUsing the os.path.abspath() function to print the current directory in PythonUsing the pathlib.Path.cwd() function to print the current directory in PythonUsing the […]

  • Python copy file to another directory
    17 January

    How to copy file to another directory in Python

    Table of ContentsWays to copy file to another directoy in PythonUsing file handlingUsing the shutil libraryUsing the pathlib libraryUsing the os moduleUsing the subprocess moduleConclusionWas this post helpful? In this article, we will see different ways to copy file to another directory in Python. We can read and write files in Python. We can also […]

  • Copy file Python
    12 January

    How to Copy File in Python?

    Table of ContentsCopy file using the shutil module in PythonCopy file using the copyfileobj() function in PythonCopy file using the copyfile() function in PythonCopy file using the copy() function in PythonCopy file using the copy2() function in PythonCopy file using the operating system command in PythonCopy file using the os module in PythonCopy file using […]

  • Python absolute path
    17 November

    Get Absolute Path in Python

    Table of ContentsWhat is an absolute path to a file? How to get absolute path in PythonUse abspath to get the absolute path to file use pathlib module to get the absolute path to file ConclusionWas this post helpful? We often use files to store the outputs of our programs in permanent storage. Generally, we […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.