Function open () in Python

The open () function is built into Python to use to open a file and return the corresponding file object. Follow the article to learn more about the syntax, parameters and usage of open ()

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

  1. file : path and name of the file
  2. mode : 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