PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » File Handling » Python Write to File

Python Write to File

Updated on: December 30, 2021 | 4 Comments

It is pretty standard that large chunks of data need to store in the Files. Python is widely used in data analytics and comes with some inbuilt functions to write data into files.

We can open a file and do different operations on it, such as write new contents into it or modify a file for appending content at the end of a file.

After reading this tutorial, you’ll learn: –

  • Writing into both text and binary files
  • The different modes for writing a file
  • Writing single or multiple lines in a file.
  • All methods for writing a file such as write() and writeline().
  • Appending new contents at the end of an existing file
  • Open file for both reading and writing.

Table of contents

  • Access Modes for Writing a file
  • Steps for Writing Data into a File in Python
    • Example: Write to a Text file in Python
    • Writing To An Existing File
  • File Write Methods
  • writelines(): Write a list of lines to a file
  • with Statement to Write a File
  • Appending New Content to an Existing File
  • Append and Read on the Same File
  • Writing to a Binary File
  • Summary

Access Modes for Writing a file

Access mode specifying the purpose of opening a file.

Whenever we need to write text into a file, we have to open the file in one of the specified access modes. We can open the file basically to read, write or append and sometimes to do multiple operations on a single file.

To write the contents into a file, we have to open the file in write mode. Open a file using the built-in function called open(). This function takes two parameters, namely filename, and access mode, and returns the file pointer.

We can open a file for modifying or overwrite its contents by using any one of the modes described in the following table.

Access ModeDescription
wOpen a file for writing. In the case of an existing file, it truncates the existing content and places the filehandle at the beginning of the file. A new file is created if the mentioned file doesn’t exist.
w+Open a file for both reading and writing. In the case of the existing file, it will truncate the entire content and place the filehandle at the beginning of the file.
wbOpen a binary file for writing. The FileHandle is placed at the beginning of the file, and existing content will be truncated. A new file is created otherwise.
aOpens a file for writing. The FileHandle will be placed at the end of the file. In the case of the existing file, the new content will be added after the existing content. A new file will be created otherwise.
a+Open a file for appending as well as reading. The FileHandle is placed at the end of the file. In the case of the existing file, the new content will get added after the existing content. A new file is created otherwise.
Python file write modes

Steps for Writing Data into a File in Python

To write into a file, Please follow these steps:

  1. Find the path of a file

    We can read a file using both relative path and absolute path. The path is the location of the file on the disk.
    An absolute path contains the complete directory list required to locate the file.
    A relative path contains the current directory and then the file name.

  2. Open file in write mode

    Pass file path and access mode w to the open() function. The access mode opens a file in write mode.
    For example, fp= open(r'File_Path', 'w')

  3. Write content into the file

    Once a file is opened in the write mode, write text or content into the file using the write() method.
    For example, fp.write('new text').
    The write() method will add new text at the beginning of a file. For an existing file, this new content will replace the existing content. If the file is not already present a new file will be created, and content is written into it.

  4. Close file after completing the write operation

    We need to make sure that the file will be closed properly after completing the file operation. Use fp.close() to close a file.

  5. Append content at the end of the file

    Pass file path and access mode a to the open() function to open a file in append mode.
    For example, fp= open(r'File_Path', 'a')
    Next, use the write() method to write content at the end of the file without deleting the existing content

Example: Write to a Text file in Python

The following code shows how to write a string into a new file. In this example, we are writing a single line into a file.

text = "This is new content"
# writing new content to the file
fp = open("write_demo.txt", 'w')
fp.write(text)
print('Done Writing')
fp.close()

# Open the file for reading the new contents
fp = open("write_demo.txt", 'r')
print(fp.read())
fp.close()Code language: Python (python)
Done Writing
This is new content

In the above example, the write_demo.txt was not present, and therefore a new file is created with that name. here we used a relative path. The content is written at the beginning.