Python file object several operation mode difference — file operation method detailed explanation

 

 

Byte mode of file object/b modeutf-8

 

Read operation

Write operation

Pointer operation

ASCII bytes

Returnbytes/ byte typeAscii

Write inbytes

For example:b’This is ascii’

Useseek set any byte each time.

Chinese string

Returnbytes/ byte type of garbled
For example:\xe4\xbd\xa0 ‘\xe4\xbd\xa0’.decode(‘utf-8’)

The string is encoded before it can be written.

For example:‘.encode(‘utf-8’)

Useseek Settings3

Text mode of file object

 

Read operation

Write operation

Pointer operation

ASCII bytes

Returns a string that can be viewed

Write to find string

Useseek set any byte each time.

Chinese string

Returns a string that can be viewed

Write to find string

Useseek Settings3

Pattern

File existence

file does not exist

operation

Document content

r

Establishopen objects normally open normally.

Establishopen object failed to read the original file.

read-only

Readable and unreadable, default isr

You can use pointer to adjust position to read.

w

EstablishopenThe contents of the original document are directly emptied.

Establishopen objects create new files normally

Only write

When the file is opened, the content is emptied. You can adjust the pointer position to write.

a

Establishopen objects normally open normally.

Establishopen objects create new files normally

Only write

Does not cover the contents of the original document. Content can only be added at the end, and the pointer can not change the location of the write. If the content is not written, the original document will remain unchanged.

r+

Establishopen objects normally open normally.

Establishopen object failed to read the original file.

Read and write

Does not cover the contents of the original document. The pointer appends the content at the end of the default, and the pointer overwrites the contents of the original file when it writes in other locations. If the content is not written, the original document will remain unchanged. Read files according to pointer location

w+

EstablishopenThe contents of the original document are directly emptied.

Establishopen objects create new files normally

Read and write

When the file is opened, the content is emptied. The write operation covers the contents of the previous file. Read files according to pointer location

a+

Establishopen objects normally open normally.

Establishopen objects create new files normally

Read and write

At the end of the pointer, the file can only be added to the file and read the file according to the pointer position. If the content is not written, the original document will remain unchanged.

Note: as long as it is a writable mode, it can be called multiple times before closing the file.writeDetailed explanation

1. Operation of documents

 The operation file is about the process, whether the file exists, open the file, read and write the file, close the file.

 

 1 >>> import os
 2 >>> os.path.isfile('wdwj') #Non-existent
 3 False
 4 >>> f=open('wdwj','w')#Open the file in'w', and automatically create it if it does not exist.
 5 >>> f.write('Contents of my files')#Writing file contents
 6 6
 7 >>> f.close()#Close the file
 8 >>> os.path.isfile('wdwj')#Has been successfully created
 9 True
10 >>> with open('wdwj') as f: #After opening files with with, the file is automatically closed.
11 ...     data=f.read()
12 ... 
13 >>> print(data)
14 Contents of my files

 

python3 open file

See/windowspycharm isutf-8staypycharmutf-8

And then runpycharm program, read this text file will be wrong, it will use.windows DefaultGBK code reads.

 

Open the file

f = open(‘musictxt’,encoding=’utf-8′)#encoding=’utf-8′

def open(file, mode=’r’, buffering=None, encoding=None, errors=None, newline=None, closefd=True):

 

Open file modeA more detailed explanation is given in the table above.

 r read mode (default).

 w write mode only.

 a additional mode.

Usually, files are opened by text, and strings of read and write files will be encoded in specific ways.UTF-8.

Mode plus‘b’ read and write files are read and written in bytes. CommonJPGEXEUnix\n , Windows\r\n

If the file is read, the pointer position is not set. The default read file location pointer moves from the file header to the end of the file, so it cannot be read repeatedly after reading once.

read file

f.read(size)size > is an optional item, specifying the length of the string.size no specified or negative number will read the entire file. When the file size is two times the memory of the current machine, it will go wrong. If the end of the file is displayed, an empty string will be displayed.

read(),readline()

1 >>> f.read()
2 'zhe shi wenjian.\n'
3 >>> f.read()
4 '' #These two single quotes are empty strings.

Read line

f.readline()‘\n’

 1 >>> f.readline()
 2 'wo shi wenjian neirong.\n'
 3 >>> f.readline()
 4 'zhe shi di er hang\n'
 5 >>> f.readline()
 6 ''
 7 
 8 #Loop traverses the file object to read the first row.
 9 >>> for line in f:
10 ...     print(line, end='')
11 ...
12 wo shi wenjian neirong.
13 zhe shi di er hang

 

If you want to read all the lines in a file to a list, you can also use it.list(f) f.readlines()

Write a document

f.write(string)string  is written to the file, and the length of the written character is returned.:

 >>> f.write(‘This is a test\n’)

15

 To write other non string content, first convert it to string.

 >>> value = (‘the answer’, 42)

>>> s = str(value)

>>> f.write(s)

18

 

Pointer operation of file objects

f.tell() method returns an integer indicating the current pointer position.

f.seek(offset,from_what) method set the pointer position.

form_what > is the starting position. The value is starts at the beginning of the document. represents the beginning of the current pointer position. indicates the beginning of the end of the document.

 1 >> f = open('workfile', 'rb+')
 2 >>> f.write(b'0123456789abcdef')
 3 16
 4 >>> f.seek(5) 
 5 5
 6 >>> f.read(1)
 7 b'5'
 8 >>> f.seek(-3, 2) 
 9 13
10 >>> f.read(1)
11 b'd'

You can manipulate this article, but notice that a string is 3 bytes. The pointer is in bytes, not 3.

Note: When both Chinese and ASCII are included in the text, it is more troublesome to generate scrambling code whether byte or string operation pointer.

For example, the following is produced.3 needs to be modified.bytes byte data to repair. operationbytes type characters, cut out bad data by slice method.

>>> b

b’abcdefg,\x8d\xe9\x9d\xa2\xe4\xba\x86,\x80\xe4\xb8\x8b\xe5\x9c\xa8\xe5\x89\x8d\xe9\x9d\xa2\xe5\x8a\xa0\xe4\xb8\x80\xe4\xb8\x8b,,try,try12345678abcdef——-hehe’

>>> b[:4]

b’abcd’

>>> b=b[:8]+b[10:]

>>> c.decode(‘utf-8’)

,,try,try12345678abcdef——-hehe’

>>> 

 

The following is a detailed example of file operation:

 1 >>> f = open('bin_file','wb') #Creating file objects in byte write mode
 2 >>> f.write(b'I am the content of the document.') #It can only be ASCII characters.
 3   File "<stdin>", line 1
 4 SyntaxError: bytes can only contain ASCII literal characters.
 5 >>> f.write(b'wo shi wenjian neirong')#It can only be ASCII characters.
 6 22 #A total of 22 byte is written.
 7 >>> f.close()
 8 >>> 
 9 (env) hehe@ubuntu:~/tmp/file_object_test$ more bin_file 
10 #asciiBytes written in the command line can be opened in text.
11 wo shi wenjian neirong
12 >>> f = open('bin_file')#asciiBytes written into files, using file objects can be opened in text.
13 >>> f.read()
14 'wo shi wenjian neirong'
15 >>> f.close()
16 >>> f = open('bin_file','rb')#asciiBytes written into files, using file objects can be opened in bytes.
17 >>> f.read()
18 b'wo shi wenjian neirong'
19 >>> f.close()
20 >>> f = open('bin_file','wb')#Create file objects in byte write mode
21 >>> f.write(b'I am the content')#When writing operations, only byte parameters can be received, and errors can be made by using string parameters.
22   File "<stdin>", line 1
23 SyntaxError: bytes can only contain ASCII literal characters.
24 >>> f.write('I am the content'.encode('utf-8'))#The string is encoded into bytes and can be written.
25 12
26 >>> f.close()
27 (env) hehe@ubuntu:~/tmp/file_object_test$ more bin_file #The command line can be opened directly in text.
28 I am the content29 >>> f = open('bin_file','r')#In interactive mode, you can use the text mode to open the Chinese string written bytes.
30 >>> f.read()
31 'I am the content'
32 >>> f.close()
33 >>> f = open('bin_file','rb')
34 >>> f.read()#Each 4 symbol ("\xb9") is a byte, and every 3 byte is a Chinese.
35 b'\xe6\x88\x91\xe6\x98\xaf\xe5\x86\x85\xe5\xae\xb9'
36 >>> f.tell()
37 12
38 >>> f.seek(0)#Because the pointer has gone to the end of the file when reading the file, it needs to move it to the beginning.
39 0
40 >>> f.read().decode('utf-8')#To open the file in byte mode, the Chinese character needs to be decoded.
41 'I am the content'
42 >>> f.seek(0)
43 0
44 >>> f.seek(1)
45 1
46 >>> f.read()#A byte was moved back, so \xe6 did not show.
47 b'\x88\x91\xe6\x98\xaf\xe5\x86\x85\xe5\xae\xb9'
48 >>> f.seek(1)
49 1
50 >>> info=f.read()
51 >>> info.decode('utf-8')#It is not enough to move a word, and the 3 byte is a Chinese.
52 Traceback (most recent call last):
53   File "<stdin>", line 1, in <module>
54 UnicodeDecodeError: 'utf-8' codec can't decode byte 0x88 in position 0: invalid start byte
55 >>> f.seek(3)
56 3
57 >>> info=f.read()
58 >>> info.decode('utf-8')#The following result is just missing one word.
59 'It's the content'
60 >>> 

 

According to the original content of python3 official manual, if you need to reprint, please indicate the source.

 

Leave a Reply

Your email address will not be published. Required fields are marked *