Python Tutorial - Open Function

Python Exercise Lists

How to open specified file using Python?

In Python, if you want to manipulate files, you first need to create or open the specified file and create a file object, and these tasks can be achieved by the built-in open() function.

The open() function is used to create or open a specified file. The common syntax format of this function is as follows:

file = open (file_name [,mode='r' [,buffering=-1 [,encoding = None]]])

In this format, the parts enclosed in [] are optional parameters, which can be used or omitted. Among them, the meaning of each parameter is as follows:

  • file: indicates the file object to be created.
  • file_mode: The file name of the file to be created or opened. The name must be enclosed in quotation marks (single or double quotes are acceptable). It should be noted that if the file to be opened is located in the same directory as the currently executing code file, you can write the file name directly; otherwise, this parameter needs to specify the full path where the file is opened.
  • mode: Optional parameter used to specify the file opening mode. The selectable open modes are shown in the following table. If not written, the file is opened in read-only(r) mode by default.
  • buffing: optional parameter, used to specify whether to use a buffer when reading and writing files (this section will be described in detail later).
  • encoding: Manually set the encoding format used when opening the file. The value of the ecoding parameter is different for different platforms. Taking Windows as an example, the default is cp936 (in fact, GBK encoding).

The file open modes supported by the open () function are shown in the following table:

Mode Meaning Precautions
r Open the file in read-only mode, and the pointer to read the content of the file will be placed at the beginning of the file. The operating file must exist.
rb Open the file in binary format and read-only mode. The pointer to read the content of the file is located at the beginning of the file. It is generally used for non-text files, such as picture files and audio files. The operating file must exist.
r+ After opening the file, you can either read the file content from the beginning or write new content to the file from the beginning. The new content written overwrites the original content of the file of medium length. The operating file must exist.
rb+ Open the file in binary format using read-write mode. The pointer for reading and writing the file will be placed at the beginning of the file, usually for non-text files (such as audio files). The operating file must exist.
w Open the file in write-only mode. If the file exists, the original content in the file will be emptied when opened. If the file exists, its original content is overwritten (overwrite the file); otherwise, a new file is created.
wb Open files in binary format, write-only mode, typically for non-text files (such as audio files) If the file exists, its original content is overwritten (overwrite the file); otherwise, a new file is created.
w+ After opening the file, the original content will be emptied, and the file has read and write permissions If the file exists, its original content is overwritten (overwrite the file); otherwise, a new file is created.
wb+ Open files in binary format, read-write mode, generally used for non-text files If the file exists, its original content is overwritten (overwrite the file); otherwise, a new file is created.
a Open a file in append mode and only have write access to the file. If the file already exists, the file pointer will be placed at the end of the file (that is, the newly written content will be after the existing content); otherwise, a new file will be created. -
ab Open the file in binary format and use append mode, only write permissions to the file. If the file already exists, the file pointer is at the end of the file (the newly written file will be after the existing content); otherwise, a new file is created. -
a+ Open the file in read-write mode; if the file exists, the file pointer is placed at the end of the file (the newly written file will be after the existing content); otherwise, a new file is created. -
ab+ Open the file in binary mode and use append mode to have read and write permissions on the file. If the file exists, the file pointer is at the end of the file (the newly written file will be after the existing content); otherwise, a new file is created -

The file opening mode directly determines what operations can be performed on the file in the future. For example, for files opened in r mode, subsequent code can only read the file and cannot modify the file contents.

[Example] The "a.txt" file is opened by default:

# Note: There is no a.txt file in the same directory as the current program file

file = open ("a.txt")
print (file)

When opening a file in the default mode, the r permission is used by default. Because the file required to be opened by this permission must exist, running this code will report the following error

#There is no a.txt file in the same directory as the current program file
file = open ("a.txt")
print (file)

The output is:

---------------------------------------------------------------------------
FileNotFoundError      Traceback (most recent call last)
[ipython-input-19-b3b69ef64e59] in [module]
----> 1 file = open ("a.txt")
2 print (file)

FileNotFoundError: [Errno 2] No such file or directory: 'a.txt'

Now, in the same directory as the program file, manually create an a.txt file and run the program again. The result is:

_io.TextIOWrapper name = 'a.txt' mode = 'r' encoding = 'cp936'

You can see that in the current output result, relevant information about the file object is output, including the name of the open file, the open mode, and the encoding format used when opening the file.

When opening a file with open (), GBK encoding is used by default. But when the file to be opened is not GBK encoding format, you can manually specify the encoding format of the open file when using the open() function, for example:

file = open ("a.txt", encoding = "utf-8")

Note that manually modifying the value of the encoding parameter is limited to opening the file as text, that is, you cannot modify the value of the encoding parameter in binary format, otherwise the program will throw a ValueError exception, as shown below:

ValueError: binary mode doesn't take an encoding argument

Does open () require a buffer area in Python?

In general, it is recommended that you open the buffer when using the open() function, that is, you do not need to modify the value of the buffing parameter.

If the value of the buffing parameter is 0 (or False), it means that the buffer is not used when opening the specified file; if the value of the buffing parameter is an integer greater than 1, the integer is used to specify the size of the buffer (in bytes); If the value of the buffing parameter is negative, the default buffer size is used.

why? The reason is very simple. So far, the I/O speed of computer memory is still much higher than the I/O speed of computer peripherals (such as keyboard, mouse, hard disk, etc.). If buffers are not used, the program is performing I / O operations. At this time, the computer memory and the peripheral must perform synchronous read and write operations, that is, the memory must wait for a byte to be input(output) by the peripheral before it can output(input) a byte again. This means that programs in memory spend most of their time waiting.

If a buffer is used, the program will first output all data to the buffer when performing the output operation, and then continue to perform other operations. The data in the buffer will be read and processed by the peripheral. When performing an input operation, it will wait for the peripheral to read the data into the buffer first, and there is no need to do synchronous read and write operations with the peripheral.

Open() File Object commonly Used Attributes in Python

After successfully opening the file, you can call the properties owned by the file object itself to obtain some information about the current file. The common properties are:

  • file.name: returns the name of the file;
  • file.mode: returns the file opening mode used when opening the file;
  • file.encoding: Returns the encoding format used when opening the file;
  • file.closed: Determine whether the file has been closed.

Example:

f = open ('a.txt')  #Open file by default (output_12)
print (f.closed)   # Whether the output file is closed
print (f.mode)   # Output access mode
print (f.encoding)  #Output encoding format
print (f.name)   # Output file name

The output is:

False
r
cp1252
a.txt

Note that the file object opened with the open() function must be closed manually (explained in the subsequent chapters). The Python garbage collection mechanism cannot automatically recycle the resources occupied by the open file.

                               


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression


More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises