Function open () in Python
The open () function is built into Python to use to open a file and return the corresponding file object.
Follow TipsMake.com to follow the article to learn more about the syntax, parameters and usage of open ().
The syntax of open () function in Python
open(file, mode)
Parameters of open () function
file
: path and name of the filemode
: string that determines the mode you want to open the file. If this parameter is not passed, Python defaults to opening the file in'r'
mode (reading the text).
Several different modes when opening a file:
MODE
DESCRIBE
'r' Mode is only allowed to read. 'r +' Mode is allowed to read and write 'rb' Open the read mode file for binary format. Cursor at the beginning of the file 'rb +' 'r + b' Open the file to read and write in binary format. Cursor at the beginning of the file 'w' Open the file to write. If the file does not exist, it will create a new file and write the content, if the file already exists, it will be truncated and overwrite the old content 'w +' Open the file to read and write. If the file does not exist, it will create a new file and write the content, if the file already exists, it will be truncated and overwrite the old content 'wb' Open the file to write to binary. If the file does not exist, it will create a new file and write the content. If the file already exists, it will be truncated and overwrite the old content 'wb +'.
'w + b' Open the file to read and write to binary. If the file does not exist, it will create a new file and write the content. If the file already exists, it will be truncated and overwrite the old content. If the file already exists, it will write the content to the end of the file, if the file does not exist then create a new file and write the content there. 'a +' Open the read and write mode files. If the file already exists, it will write the content to the end of the file, if the file does not exist then create a new file and write the content there. ab 'Open the recording mode file in binary format. If the file already exists, it will write the content to the end of the file, if the file does not exist then create a new file and write the content there. 'ab +'
'a + b' Open the read and write mode files in binary format. If the file already exists, it will write the content to the end of the file, if the file does not exist then create a new file and write the content there. 'x' Open the recording mode file. Create new exclusive file and write the content, if the file already exists, the program will report 'x +' error Open the read and write mode file. Create new exclusive file and write the content, if the file already exists, the program will report 'xb' error. Open binary file mode. Create new proprietary file and write the content, if the file already exists, the program will report 'xb +' error
'x + b' Open the read and write file in binary format. Create a new proprietary file and write the content, if the file already exists, the program will issue an error 'b' Open the file in binary mode 't' Open the file in text mode (default)
Value returned from open ()
The open () function returns a file object called "handle" because you can perform read, write, modify operations on the file.
If the file is not found, Python will generate a FileNotFoundError exception .
Example 1: open () works like?
# mở file cùng thư mục với file hiện tại # viết bởi TipsMake.com f = open("quantrimang.txt") # mở file ở thư mục khác, đường dẫn đầy đủ f = open("C:/Python33/README.txt")
In this example we omit the mode parameter, so Python accesses the default file as read (r). When using this mode we will get the string value returned as text.
Example 2: open () has a mode parameter
# mở ở chế độ đọc f = open("test.txt", mode='r') # mở ở chế độ ghi f = open("test.txt", mode = 'w') # mở ở chế độ ghi tiếp f = open("test.txt", mode = 'a') # mở ở chế độ đọc và ghi dạng nhị phân f = open("img.bmp",'r+b')
When working with files in text mode, you should specify the encoding type.
f = open("test.txt",mode = 'r',encoding = 'utf-8')
See also: Built-in Python functions
5 ★ | 1 Vote
You should read it
May be interested
- The iter () function in Pythoncontinuing with the topic of python's built-in functions, the article will introduce you to the iter () function with the syntax, usage as well as specific examples. invites you to read the track.
- Max () function in Pythonpython's built-in max () function returns the largest element in an iterable or the largest of the passed parameters
- Min () function in Pythonthe built-in min () function in python returns the smallest element in an iterable or smallest of passed parameters. so, how does the syntax of min () function work, what parameters and how does it work?
- Format () function in Pythonthe format () function is built into python to use to format an input value into a specific format.
- Help () function in Pythonthe built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
- Isinstance () function in Pythonthe built-in isinstance () function in python checks whether an object is an instance or a subclass of classinfo.