Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of mode Mode when python open reads File contents

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge points of mode mode instance analysis when python open reads the contents of the file, the content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Python can use the open function to open, close, read and write files.

The open function in Python3 is defined as: open (file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True), where the mode list is:

'r'# open for reading (default)

'w' # open for writing, truncating the file first

New to 'x' # create a new file and open it for writing,python3

'a'# open for writing, appending to the end of the file if it exists

'b' # binary mode

't' # text mode (default), added to python3

'+' # open a disk file for updating (reading and writing)

'U' # universal newline mode (deprecated)

Here we are mainly concerned about the reading and writing operations of different modes of reading and writing, which are mainly concerned with the reading and writing of different modes.

'r'

Read-only mode, the default mode of the mode parameter in the open function. If the file does not exist, report to FileNotFoundError (python2 is IOError)

After the file is opened, the initial cursor position is 0

Every time I read it, I start from the cursor position.

If a write operation is performed, the following exception is reported:

Io.UnsupportedOperation: not writable

'w'

Write-only mode, if the file does not exist, create the file; if the file exists, first empty the file, and then start writing

After the file is opened, the initial cursor position is 0

Every time I write, I start from the cursor position.

If a read operation is performed, the file will be emptied first, and the following exception will be reported:

Io.UnsupportedOperation: not readable

'a'

Append mode. Create a file if the file does not exist; if the file exists, it will not empty the file.

After the file is opened, the initial cursor position is at the end of the file

Every time I write, I start at the end.

If a read operation is performed, the following exception is reported:

Io.UnsupportedOperation: not readable

The above is easier to understand, but it's a little bit roundabout below.

'ringing'

Read-write mode. If the file does not exist, report to FileNotFoundError (python2 is IOError)

After the file is opened, the initial cursor position is 0

Each read and write starts at the cursor position; but for write operations, it is similar to a replace operation

Look at the following code:

The content of the file is: abcdefg

The code is as follows:

F = open ('open_mode.txt',' ritual') f.write ('xyz') f.close ()

After running the code, the file content changes to: xyzdefg

'wicked'

Write-only mode, if the file does not exist, create the file; if the file exists, first empty the file

After the file is opened, the initial cursor position is 0

Each read and write starts at the cursor position; a write operation, similar to a replace operation

'Another'

Append mode. Create a file if the file does not exist; if the file exists, it will not empty the file.

After the file is opened, the initial cursor position is at the end of the file

Every time I write, I start at the end.

The read operation starts at the cursor position

'x'

New addition of python3

Create a file and write the operation. The operation must be a non-existent file. If the file already exists, an error FileExistsError will be reported.

Unreadable, if a read operation is performed, the following exception is reported:

Io.UnsupportedOperation: not readable

'b'

Read and write files in binary form

When writing data, the data type must be a string type, and other types must be written through json (that is, strings that conform to json format).

Python2 and python3 behave differently for'b 'mode, which is related to the string types of python2 and python3; in fact, there are two string types of python

The two string types of Python2, called str and Unicode,str, contain the original 8-bit value, while the instance of unicode contains the Unicode character.

The two string types of Python2, called byte and str, unlike python2, the byte instance contains the original 8-bit value, while the instance of str contains the Unicode character.

When the read-write file is in'b 'mode, it must be read and written in binary form. In python2, the string must be a str string, and in python3 it must be a byte string; so read and write strings like this in python3,'b' mode

S = b'hello worldview'# Note is the byte string f = open ('open_mode.txt','wb') f.write (s)

Or:

S = 'hello worldview f.write = open (' open_mode.txt','wb') f.write (s.encode (encoding='utf-8'))

Otherwise, the following error will be reported:

Traceback (most recent call last):

File "C:/Users/Desktop/Python/cnblogs/ data type .py", line 125, in f.write (s)

TypeError: a bytes-like object is required, not 'str'

These are all the contents of the article "mode schema instance analysis when python open reads the contents of a file". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report