This tutorial covers the following topic – Python Write File/Read File. It describes the syntax of the writing to a file in Python. Also, it explains how to write to a text file and provides several examples for help.

For writing to a file in Python, you would need a couple of functions such as Open()Write(), and Read(). All these are built-in Python functions and don’t require a module to import.

There are majorly two types of files you may have to interact with while programming. One is the text file that contains streams of ASCII or UNICODE (UTF-8) characters. Each line ends with a newline (“\n”) char, a.k.a. EOL (End of Line).

Another type of file is called binary that contains machine-readable data. It doesn’t have, so-called line as there is no line-ending. Only the application using it would know about its content.

Anyways, this tutorial will strictly tell you to work with the text files only.

Python Write File Explained with Examples

Let’s begin this tutorial by taking on the first call required to write to a file in Python, i.e., Open().

Open File in Python

You first have to open a file in Python for writing. Python provides the built-in open() function.

The open() function would return a handle to the file if it opened successfully. It takes two arguments, as shown below:

''' Python open file syntax '''
 file_handle = open("file_name", "access_mode")

The first argument is the name or path of the file (including file name). For example – sample_log.txt or /Users/john/home/sample_log.txt.

And the second parameter (optional) represents a mode to open the file. The value of the “access_mode” defines the operation you want to perform on it. The default value is the READ only mode.

# Open a file named "sample_log.txt" 
# It rests in the same directory as you are working in. 
file_handle1 = open("sample_log.txt")

# Let's open the file from a given path
file_handle2 = open("/Users/john/home/sample_log.txt")

File Open Modes

It is optional to pass the mode argument. If you don’t set it, then Python uses “r” as the default value for the access mode. It means that Python will open a file for read-only purpose.

However, there are a total of six access modes available in python.

  • “r” – It opens a text file for reading. It keeps the offset at the start of the file. If the file is missing, then it raises an I/O error. It is also the default mode.
  • “r+” – It opens the file for both READ and WRITE operations. It sets the offset at the start of the file. An I/O error occurs for a non-existent file.
  • “w” – It opens a file for writing and overwrites any existing content. The handle remains at the start of the data. If the file doesn’t exist, then it creates one.
  • “w+” – It opens the file for both READ and WRITE operations. Rest, it works the same as the “w” mode.
  • “a” – It opens a file for writing or creates a new one if the file not found. The handle moves to the end (EOF). It preserves the existing content and inserts data to the end.
  • “a+” – It opens the file for both READ and WRITE operations. Rest, it works the same as the “a” mode.

Check out a few examples:

# Open a file named "sample_log.txt" in write mode
###
# It rests in the same directory as you are working in.
file_handle1 = open("sample_log.txt", "w")

# Open the file from a given path in append mode
file_handle2 = open("/Users/john/home/sample_log.txt", "a")

#python tutorials #python #programming

Read/Write File in Python with Examples
1.55 GEEK