File handling in Python (also known as Python I/O) involves the reading and writing process and many other file handling options. The built-in Python methods can manage two file types, text and binary files, but they encode data differently.
A text file consists of a series of lines. And each text line consists of several characters. The end of the line (EOL) signifies the ending of each line in a text file. There are several special characters used as EOL, but the most common are comma {,} and new lines.
How to write to a file using Python?
Take an example to understand the standard steps used during File Handling in Python.
• Opening a file to write.
• Appending and writing to a file.
• Closing a file
Why File Handling Is Important In Python?
The concept of file handling can also use in other programming languages, but it is complicated or time-consuming. Unlike other programming languages, it is easy to implement Python file handling. Also, it is important to know how to read the data from a given file whenever you want to analyses the data. All of these make the file handling concept extremely important.
Python offers built-in functions to open, write, read, close and delete files. Let us start by opening a file.
Open a file in Python
To read or write to a file, you must first open it. The built-in Python function open() used to open a file. This function takes two arguments, one file name, and the other mode (read mode, write mode) to open the file. It returns a file object that is used together with different functions. We use the following syntax to open a file in python.
Syntax of the Python open function:
file object = open(file_name [, access_mode][, buffering])
Below are the parameter details.
<file_name> – It’s a string refers to the file you want to access.
<access_mode> – The access_mode sets the mode to open the file,e.g., read, write, append, and so on. The parameter is optional. It is set to read-only <r> by default. In this mode, after reading from the file, we get data in text form.
The binary mode, on the other hand, returns bytes. It is best to access non-text files such as an image or Exe files.
<buffering> – The buffer indicates whether or not a buffer made. The default value is 0, meaning that buffering will not occur. When the value is 1, line buffering occurs when you access the file. If it is more than 1, the buffer action will execute according to the buffer size. The default behaviour considered in the case of a negative value.
Python file modes
When working with the Python file, you have to use modes for specific operations such as creating, reading, writing, adding etc. It referred to Python file modes in file handling.
Modes | Description |
r | Opens a file for reading only. |
rb | Opens a file for reading only in binary format. |
r+ | Opens a file for both reading and writing. |
rb+ | Opens a file for both reading and writing in binary format. |
w | Opens a file for writing only. |
wb | Opens a file for writing only in binary format. |
w+ | Opens a file for both writing and reading. |
wb+ | Opens a file for both writing and reading in binary format. |
a | Opens a file for appending. |
ab | Opens a file for appending in binary format. |
a+ | Opens a file for both appending and reading. |
ab+ | Opens a file for both appending and reading in binary format. |
Take a look at this program and analyse how the reading mode works:
# a file named "file", will be opened with the reading mode. file = open('file.txt', 'r') # Each line will be printed one by one in the file for each in file: print (each)
The open command opens the file in reading mode and prints each line of the file with for loop.
In Python, there is more than one way to read a file. If you need to extract a string with all of the characters in the file, we can use the.read(). The entire code works like this:
# Python code to illustrate read() mode file = open("file.text", "r") print (file.read())
Another way to read a file is to call a number of characters like in the code below. The interpreter will read and return the first five characters of the stored data as a string:
# Python code to illustrate read() mode character wise file = open("file.txt", "r") print (file.read(5))
The Python file object attributes
When you call the Python open() function, an object that is the filehandle is returned. You should also know that Python files have several attributes linked. And we can use the file handle to list the file attributes.
Please run through the table below to learn more about the file attributes.
Attribute | Description |
<file.closed> | For a closed file, it returns true whereas false otherwise. |
<file.mode> | Returns the file opening access mode. |
<file.name> | Returns the file name. |
<file.softspace> | Returns a boolean to suggest if a space char is added in the output <print> command before it is printed another values. |
Example: Python file attribute in action
#Open a file in write and binary mode. file = open("app.log", "wb") #Display file name. print "File name: ", file.name #Display state of the file. print "File state: ", file.closed #Print the opening mode. print "Opening mode: ", file.mode #Output the softspace value. print "Softspace flag: ", file.softspace
File name: app.log File state: False Opening mode: wb Softspace flag: 0
Perform Write operation
First of all, open it with a mode (read/write/append) while you are ready to enter data to a file.
You can even do the same with the mode append. If you have also used the <w> mode, the existing data from the file will delete. It would help if you, therefore, took note of this fact when you choose it.
The write() file method
Python provides a write() method to write a string or bytes sequence to a file. This function returns a number that is the size of data stored in a single Write call.
Example: Read/Write to a File in Python
# Python code to create a file file = open('file.txt','w') file.write("This is the write command") file.write("It allows us to write in a particular file") file.close()
The close() command ends all used resources and frees the system from the specific program.
In the above example, the statement f=open(“file.txt”, “w”) opens file.txt in write mode, returns the file object and assigns it to the variable f in the open method. “w” specifies the writable file. Next, we’ve got to put some data into the file. In the file, a string saved in the file.write(“Hello! Python”). Finally, file.close() closes the object file.
You find “file.txt” created on your computer when you run the above code. You can view the contents by opening an editor, such as Notepad.
Python provides the writelines() method for saving the contents of the list object in a file. Since the character of the new line is not automatically written to the file, the character must be given in the string.
Example: Write Lines to File
lines=["Hello world.\n", "Welcome to Technology.\n"] f=open("D:\file.txt","w") file.writelines(lines) file.close()
Close a file in Python
When your work finished, it is always the best practice to close a file. Python, however, runs a garbage collector to clean the unused objects.
The close() file method
Python provides the <close()> method to close a file.
Close operation in Python
The most basic way is to call the close() method of Python.
file = open("app.log",encoding = 'utf-8') file.close() # do file operations.
Close with try-catch
Say, if there is an exception during certain operations on the file. In this case, the code leaves the file without closing. It’s, therefore, better to insert the code into a <try-finally> block.
try: file = open('app.log', encoding = 'utf-8') # do file operations. finally: file.close()
Even if there is an exception, the above code ensures that your file is closed properly.
Auto close using ‘with’
The WITH clause is another way of closing a file. It ensures that when the block executes within the WITH clause, the file is closed. The beauty of this method is that the close() method does not need to call explicitly.
with open('app.log', encoding = 'utf-8') as file: #do any file operation.
Perform Read operation in python
First of all, to read data from a file, you need to open it in reading mode. You can then call any of the Python methods for reading from a file.
Three different methods have provided to read file data.
• readline(): read characters from the current reading position to the newline character.
• read(chars): reads the number of characters that are specified starting from the current position.
• readlines(): reads all lines until file end and returns an object list.
Usually, we use Python <read(size)> to read the file content up to the size. If you don’t pass the size, then the whole file will read.
Example: Read from a File in Python
with open('app.log', 'w', encoding = 'utf-8') as f: file.write('my first file\n') #first line file.write('This file\n') #second line file.write('contains three lines\n') #third line f = open('app.log', 'r', encoding = 'utf-8') print(file.read(10)) # read the first 10 data #'my first f' print(file.read(4)) # read the next 4 data #'file\n' print(file.read()) # read in the rest till end of file #'This file\ncontains three lines\n' print(file.read()) # further reading returns empty sting #''
Example: Reading Lines
file = open("file.txt","r") line = file.readline() print(line) file.close()
We must open the file in ‘r’ mode. The readline() method returns the first line and points to the second line in the file.
Use the While Loop to read all lines from a file, as shown below.
Example: Reading Lines
file = open("D:\file.txt","r") line = file.readline() while line!='': print(line) line = file.readline()
Working of append() mode
Let’s see how the append mode works:
# Python code to illustrate append() mode file = open('file.txt','a') file.write("This will add this line") file.close()
There are also several other commands in File handling used to handle multiple tasks such as:
Rstrip(): This function removes spaces from the right side for each line of a file. Lstrip(): This function removes spaces from the left side of each file line.
It is intended to provide much cleaner syntax and exceptions handling when working with code. It explains why it is good to practise, if applicable, to use them with a statement. It is useful because any opened files will be automatically closed after one is done automatically by this method.
Example:
# Python code to illustrate with() with open("file.txt") as file: data = file.read() # do something with data
Using write along with with() function
We can also use write function along with with() function:
# Python code to illustrate with() alongwith write() with open("file.txt", "w") as f: file.write("Hello World!!!")
split() using file handling
We can also split lines with Python file handling. This splits the variable when there is space. You can split by any character as we like. The code is here:
# Python code to illustrate split() function with open("file.text", "r") as file: data = file.readlines() for line in data: word = line.split() print (word)
Seek() Method
Use the seek() function to set the current read/write position to read or write at a certain position.
Syntax:
file.seek(offset[, from])
The <offset> argument represents the size of the displacement.
The <from> argument indicates the start point.
If from is 0, then the shift will start from the root level.
If from is 1, then the reference position will become the current position.
It from is 2, then the end of the file would serve as the reference position.
Assuming that file.txt contains “Hello World” text, the following example demonstrates the seek() method.
Example: seek()
file = open("D:\file.txt","r+") file.seek(6,0) lines = file.readlines() for line in lines: print(line) file.close()
The output of the above example is "World".
Python File object methods
Function | Description |
<file.close()> | Close the file. You must reopen it for additional access. |
<file.flush()> | Flush the internal buffer. It’s same as the <stdio>’s <fflush()> function. |
<file.fileno()> | Returns the descriptor of an integer file. |
<file.isatty()> | It returns true if file has a <tty> attached to it. |
<file.next()> | Returns the next line from the last offset. |
<file.read(size)> | Reads the given no. of bytes. It may read less if EOF is hit. |
<file.readline(size)> | It’ll read an entire line from the file. |
<file.readlines(size_hint)> | It calls the <readline()> to read until EOF. |
<file.seek(offset[, from])> | Sets the file’s current position. |
<file.tell()> | Returns the file’s current position. |
<file.truncate(size)> | Truncates the file’s size. |
<file.write(string)> | It writes a string to the file. |
<file.writelines(sequence)> | Writes a sequence of strings to the file. |