Python provides inbuilt functions for handling files, i.e., creating, reading, writing, etc. Python handles two types of files. They are Text and binary files.
- Text file – In a text file, each line of Text is terminated by the end of Line (EOL) operator, a special character ‘\n’ in Python by default.
- Binary file – The binary file contains the binary data of forms 0’s and 1’s. Machines understand binary language.
Also Read: Understand Python Lambda Functions With Examples
How To Open a File in Python
The open() method in Python opens a file in a specified path and mode. Below is the syntax of the open() method to open a file –
open(‘file_name’,’Access_mode’)
Also Read: Python Zip() Function Tutorial With Code Examples
Different access modes to open a file in Python
1. file_name
Specifies the name of the file to open. If the Python script is not present in the same directory/folder of the file which needs to be opened, then the file’s path also needs to be specified along with the file name.
2. Access_mode
Specifies the access mode of the opened file. Below listed are the available access modes.
Also Read: Quicksort Implementation in Python
3. Read only
It opens the specified file in read mode. This access mode is represented as “r.” If any access mode is not specified in the open() method, then the file will be opened in read mode by default. If the file specified is not present, it throws an error.
4. Write only
It opens the specified file in write mode. This access mode is represented as “w.” If the specified file is not present, it creates a file with the specified name. If the specified file is already present, it truncates the data and overwrites it with new data.
5. Append only
It opens the specified file in write mode. This access mode is represented.” as “a.” If the specified file is not present, it creates a file with the specified name. If the specified file is already present, then it appends the new data from the end of already existing data.
Also Read: How To Create a Python Web Server
6. Read and write
It opens the specified file in read and writes mode. This access mode is represented as “r+.” If the file specified is not present, then it throws the error. If the specified file is already present, it truncates the data and overwrites it with new data.
7. Write and read
It opens the specified file in write and read mode. This access mode is represented as “w+.” If the specified file is not present, it creates a file with the specified name. If the specified file is already present, it truncates the data and overwrites it with new data.
8. Append and read
It opens the specified file in write and read mode. This access mode is represented as “a+.” If the specified file is not present, it creates a file with the specified name. If the specified file is already present, then it appends the new data from the end of already existing data.
9. Read only in binary format
It opens the specified file for reading in binary format. This access mode is represented as “rb.” If any access mode is not specified in the open() method, then the file will be opened in read mode by default. If the file specified is not present, it throws an error.
Also Read: 15 Most Common Python Array Programs
10. Write only in binary format
It opens the specified file for writing in binary format. This access mode is represented as “wb.” If the specified file is not present, it creates a file with the specified name. If the specified file is already present, it truncates the data and overwrites it with new data.
11. Append only in binary format
It opens the specified file for writing in binary format. This access mode is represented as “ab.” If the specified file is not present, it creates a file with the specified name. If the specified file is already present, then it appends the new data from the end of already existing data.
12. Read and write in binary format
It opens the specified file for reading and writing in binary format. This access mode is represented as “rb+.” If the file specified is not present, then it throws the error. If the specified file is already present, it truncates the data and overwrites it with new data.
13. Write and read in binary format
It opens the specified file for reading and writing in binary format. This access mode is represented as “wb+.” If the specified file is not present, it creates a file with the specified name. If the specified file is already present, it truncates the data and overwrites it with new data.
Also Read: Pattern Programs in Python [Star and Alphabetical Patterns]
14. Append and read in binary format
It opens the specified file for reading and writing in binary format. This access mode is represented as “ab+.” If the specified file is not present, it creates a file with the specified name. If the specified file is already present, then it appends the new data from the end of already existing data.
Here is a simple program that uses the open() method to open a file with a particular access mode.
Code:
# opening a file to add data to a file which already has some data and also to read the data. fileObj = open( “demo.txt” , “a+”) # append new data fileObj.write(“ - Himanshu”) # print the data in the file print(fileObj.read()) # closing the file fileObj.close()
File demo.txt before execution of code.
File demo.txt after execution of code.
Also Read: Graph Plotting in Python Using Matplotlib
Opening a file in read mode
To open a file in read mode in Python, you can use the following code:
f = open("filename.txt", "r")
This will open the file filename.txt in read mode and assign the file object to the variable f.
You can then read the file’s contents using f.read() or f.readline().
Here is an example of how you can use f.read() to read the entire contents of the file.
f = open("filename.txt", "r") contents = f.read() print(contents) f.close()
Be sure to close the file when you are done with it using f.close(). This will free up system resources associated with the file.
Also Read: Intro to Threading in Python [Understand With Examples]
Summary
Using the open() function, we opened a demo.txt file in append and read mode by passing the “a+” access mode. So it opens the specified file in write and read mode. Any data we write into the file using the script will be later appended to the data if already present. The write() & read() methods help to write any data into the file and read the data present in the file.
Other Python tutorials:
- How To Automate Google Search With Python
- Sending Emails Using Python With Image And PDF Attachments
- How To Use Python For Browser Games Development?
- How to Convert Binary to Decimal in Python [5 Methods]
- 7 Best Python IDE for Windows [Code Editors]
- How To Create Keylogger In Python
Hello Friends! I am Himanshu, a hobbyist programmer, tech enthusiast, and digital content creator.
With CodeItBro, my mission is to promote coding and help people from non-tech backgrounds to learn this modern-age skill!