'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)
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 .
# 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.
# 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