Python read file example

Language:
Python
69 views
0 favorites
2025年9月19日

Code Implementation

Python
with open("filePath","r",encoding="utf-8") as file:
    file_content=file.read()
    print(file_content)

Use Python to read a file or read it line by line.

#File Reading

Snippet Description

The open() function is a built-in Python function used to open files. Its parameters are as follows:

  • file: The file name or file path. It can be an absolute path or a relative path. If it is a relative path, it is relative to the current working directory. If the path is omitted, the file will be opened in the current working directory.

  • mode: The file opening mode. It can be one of the following values:

    • 'r': Read-only mode. This is the default mode. If the file does not exist, an exception will be raised.
    • 'w': Write mode. If the file does not exist, a new file will be created. If the file already exists, its contents will be cleared and new content will be written.
    • 'x': Exclusive creation mode. If the file does not exist, a new file will be created. If the file already exists, an exception will be raised.
    • 'a': Append mode. If the file does not exist, a new file will be created. If the file already exists, new content will be added to the end of the file.
    • 'b': Binary mode. It is used together with other modes, such as 'rb' or 'wb'.
    • 't': Text mode. It is used together with other modes, such as 'rt' or 'wt'.
  • buffering: Sets the size of the buffer. If omitted or set to 0, no buffering will be performed. If set to 1, line buffering will be used. If set to a value greater than 1, it represents the buffer size.

  • encoding: The encoding format used to encode and decode the file content. If omitted, the default encoding will be used.

  • errors: The handling method when errors occur during the encoding or decoding of file content. It can be one of the following values:

    • 'strict': Default value, which means an exception will be raised when an error occurs.
    • 'ignore': Ignores the error.
    • 'replace': Replaces the problematic character with '?'.
    • 'backslashreplace': Replaces the problematic character with a backslash escape sequence.
    • 'xmlcharrefreplace': Replaces the problematic character with an XML entity.
    • 'namereplace': Replaces the problematic character with a \N{...} escape sequence.
  • newline: Controls how line breaks are handled in text mode. It can be one of the following values:

    • None: Uses the default line break \n.
    • '': No line break conversion is performed.
    • '\n', '\r', '\r\n', '\u2028', '\u2029': Uses the specified line break.
  • closefd: If set to True, it means the underlying file descriptor will be closed when the file is opened. The default value is True.

  • opener: A custom function or class used to open the file. The default value is None.

These parameters can be used in different combinations to meet different operational needs for files. For example, open('example.txt', 'w') opens a file named example.txt in write mode. If the file does not exist, a new empty file will be created.

Comments

Loading...