Quick Reach
The open method of Python
Python comes up with a built-in function open() that enables programmers to perform open, read, write and other operations with files.
In order to perform the read or write actions in the files, first it has to be opened. Once a file is opened by using the python open() method, that creates a file object. After that, associated actions can be performed on those file(s).
Syntax of open() method
Following is the syntax of open file method:
File_object = open(file_path [, access mode] [, buffer])
Where file path is the path of the file to be opened.
Access mode defines the mode of file – read, write, append etc. This is an optional parameter. The ‘r’ is the default mode. See the last part of this tutorial for access mode parameters and its descriptions.
If buffer value is set to 0, no buffering will take place. If the value is set to 1 line, buffering will be performed.
Example of Python open and read file
The following example will open a text file, located at the same directory where the source file is located. The program will open the file, read by using Python read file method and print the text of the test.txt file.
For that, suppose test.txt contains:
“This is open file method!
The example shows open and python read the file.”
1
2
3
4
5
6
7
8
9
10
11
|
# First opening file
file_object = open(“test.txt”, “r+”)
test_text = file_object.read();
print (“The file contains : “, test_text)
# Closing opened file
file_object.close()
|
The output will be:
The file contains: This is open file method!
The example shows open and python read the file.
Python write to file example
The following example shows how to use the write to file method in Python. For this example, suppose test.txt file is empty, which is placed at the same location where Python code file resides.
First of all, the write to file step will occur where we will use the open and write methods.
After that, the file will be closed by using Python close method.
Once again, the open() method will be used to open and read the file and print the written text by using the read file method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# First opening file
file_object = open(“test.txt”, “r+”)
file_object.write (“This is Python open file method! nThe example shows open and write to file.”)
# Closing opened file
file_object.close()
#Opening file to print text written above
file_object = open(“test.txt”, “r+”)
test_text = file_object.read();
print (“The file contains : “, test_text)
|
The output will be:
The file contains: This is Python open file method!
The example shows open and write to file.
Python file open Access modes
As shown in the syntax of the open method, you can use Access mode to define the desired action with the file. Access modes with short description are listed below:
- r = opens the file as read-only.
- rb = Read only and in binary format
- w = write-only mode
- wb = write only in binary format
- r+ = read and write mode
- rb+ = read and write in binary mode
- a = opens the file in append mode. If the file exists the pointer goes at the end.
- ab = file in append and binary mode
- a+ = append and read mode
- ab+ = append and read in binary mode
Also see – Python user input
Leave A Comment?