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

  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

May be interested

  • The function set () in PythonThe function set () in Python
    in this article, tipsmake.com will learn with you about set (), syntax, parameters and specific examples. invites you to read the track.
  • Help () function in PythonHelp () function in Python
    the built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
  • Sum () function in PythonSum () function in Python
    the built-in function sum () in python returns the sum of all numbers in iterable.
  • The slice () function in PythonThe slice () function in Python
    the slice () function in python returns a slice object that helps you determine how to cut an existing string.
  • The chr () function in PythonThe chr () function in Python
    in python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.
  • The iter () function in PythonThe iter () function in Python
    continuing 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.
  • The pow () function in PythonThe pow () function in Python
    the pow () function built into python returns the value of x with the power of y (xy). if there is a third parameter, the function returns x to the power y, the module z.
  • The reversed () function in PythonThe reversed () function in Python
    the reversed () function is one of the built-in functions in python, used to reverse the original string returning the iterator.
  • Built-in Python functionsBuilt-in Python functions
    in the previous article, you learned that python has a basic function type, one is a built-in function, two are user-defined functions. today, we will learn about a list of python's built-in functions.
  • Exec () function in PythonExec () function in Python
    the exec () function used to execute python programs dynamically can be string or object code. how does exec () function syntax, what parameters do it have, and how is it used? invites you to read the track.