File Handling in Python
In this tutorial, you'll learn file handling in Python, file operations such as opening a file, reading from it, writing into it, closing it, renaming a file, deleting a file, and various file methods.
To store data temporarily and permanently, we use files. A file is the collection of data stored on a disk in one unit identified by filename.
File Handling Series
This Python file handling series contains the following in-depth tutorial. You can directly read those.
- Create File in Python: You'll learn to create a file in the current directory or a specified directory. Also, create a file with a date and time as its name. Finally, create a file with permissions.
- Open a File in Python: You'll learn to open a file using both relative and absolute paths. Different file access modes for opening a file read and write mode.
- Read File in Python: You'll learn to read both text and binary files using Python. The different modes for reading the file. All methods for reading a text file such as
- Write to File Python: You'll learn to write/append content into text and binary files using Python. All methods for writing a text to file, such as
- File Seek(): Move File Pointer Position: You'll learn to use the
seek()function to move the position of a file pointer while reading or writing a file.
- Rename Files in Python: You'll learn to rename a single file or multiple files. Renaming files that match a pattern. Rename all the files in a folder.
- Delete Files and Directories in Python: You'll learn to delete files using the os module and pathlib module. Remove files that match a pattern (wildcard). Delete a directory and all files from it
- Copy Files and Directories in Python: You'll learn to use the os, shutil, and subprocess modules to copy files and folders from one location to another.
- Move Files Or Directories in Python: You'll learn to move single and multiple files. Also, move files that match a pattern (wildcard) or move an entire directory
Types of File
- Text File: Text file usually we use to store character data. For example, test.txt
- Binary File: The binary files are used to store binary data such as images, video files, audio files, etc.
A file path defines the location of a file or folder in the computer system. There are two ways to specify a file path.
- Absolute path: which always begins with the root folder
- Relative path: which is relative to the program's current working directory
The absolute path includes the complete directory list required to locate the file.
/user/Pynative/data/sales.txt is an absolute path to discover the sales.txt. All of the information needed to find the file is contained in the path string.
After the filename, the part with a period(.) is called the file's extension, and that tells us the type of file. Here, project.pdf is a pdf document.
To read or write a file, we need to open that file. For this purpose, Python provides a built-in function
Pass file path and access mode to the
open(file_path, access_mode) function. It returns the file object. This object is used to read or write the file according to the access mode.
Accesss mode represents the purpose of opening the file. For example, R is for reading and W is for writing
In this article, we will use the test.txt file for manipulating all file operations. Create a text.txt on your machine and write the below content in it to get started with file handling.
Welcome to PYnative.com This is a sample.txt Line 3 Line 4 Line 5
# Opening the file with absolute path fp = open(r'E:\demos\files\sample.txt', 'r') # read file print(fp.read()) # Closing the file after reading fp.close() # path if you using MacOs # fp = open(r"/Users/myfiles/sample.txt", "r")
Welcome to PYnative.com This is a sample.txt Line 3 Line 4 Line 5
- Create File in Python:
- Open a File in Python
- Read File in Python
- Read Specific Lines From a File in Python
When we open a file, the operating system gives a handle to read and write a file. Once we have done using the file, it is highly recommended to close it. Because a closed file reduces the risk of being unwarrantedly modified, it will also free up the resources tied with the file.
File Access Modes
The following table shows different access modes we can use while opening a file in Python.
||It opens an existing file to read-only mode. The file pointer exists at the beginning.|
||It opens the file to read-only in binary format. The file pointer exists at the beginning.|
||It opens the file to read and write both. The file pointer exists at the beginning.|
||It opens the file to read and write both in binary format. The file pointer exists at the beginning of the file.|
||It opens the file to write only. It overwrites the file if previously exists or creates a new one if no file exists with the same name.|
||It opens the file to write only in binary format. It overwrites the file if it exists previously or creates a new one if no file exists.|
||It opens the file to write and read data. It will override existing data.|
||It opens the file to write and read both in binary format|
||It opens the file in the append mode. It will not override existing data. It creates a new file if no file exists with the same name.|
||It opens the file in the append mode in binary format.|
||It opens a file to append and read both.|
||It opens a file to append and read both in binary format.|
Writing to a File
To write content into a file, Use the access mode
w to open a file in a write mode.
- If a file already exists, 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.
- If you want to add content at the end of the file, use the access mode
ato open a file in append mode
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()
Move File Pointer
The seek() method is used to change or move the file's handle position to the specified location. The cursor defines where the data has to be read or written in the file.
The position (index) of the first character in files is zero, just like the string index.
f = open("sample.txt", "r") # move to 11 character f.seek(11) # read from 11th character print(f.read())
PYnative.com This is a sample.txt Line 3 Line 4 Line 5
tell() method to return the current position of the file pointer from the beginning of the file.
f = open("sample.txt", "r") # read first line f.readline() # get current position of file handle print(f.tell()) # Output 25
Read More: Complete Guide on File Seek(): Move File Pointer Position
Python File Methods
Python has various method available that we can use with the file object. The following table shows file method.
||Returns the file content.|
||Read single line|
||Read file into a list|
||Resizes the file to a specified size.|
||Writes the specified string to the file.|
||Writes a list of strings to the file.|
||Closes the opened file.|
||Set file pointer position in a file|
||Returns the current file location.|
||Returns a number that represents the stream, from the operating system's perspective.|
||Flushes the internal buffer.|
There are several ways to cop files in Python. The
shutil.copy() method is used to copy the source file's content to the destination file.
import shutil src_path = r"E:\demos\files\report\profit.txt" dst_path = r"E:\demos\files\account\profit.txt" shutil.copy(src_path, dst_path) print('Copied')
In Python, the os module provides the functions for file processing operations such as renaming, deleting the file, etc. The os module enables interaction with the operating system.
The os module provides
rename() method to rename the specified file name to the new name. The syntax of
rename() method is shown below.
import os # Absolute path of a file old_name = r"E:\demos\files\reports\details.txt" new_name = r"E:\demos\files\reports\new_details.txt" # Renaming the file os.rename(old_name, new_name)
In Python, the os module provides the
remove() function to remove or delete file path.
import os # remove file with absolute path os.remove(r"E:\demos\files\sales_2.txt")
Working With Bytes
A byte consists of 8 bits, and bits consist of either 0 or 1. A Byte can be interpreted in different ways like binary octal, octal, or hexadecimal. Python stores files in the form of bytes on the disk.
When we open a file in text mode, that file is decoded from bytes to a string object. when we open a file in the binary mode it returns contents as bytes object without decoding.
Now let's see the steps to write bytes to a file in Python.
- Open the file in binary write mode using
- Specify contents to write in the form of bytes.
- Use the
write()function to write byte contents to a binary file.
bytes_data = b'\x21' with open("test.txt", "wb") as fp: # Write bytes to file fp.write(bytes_data)