python basic day3

Common ways of manipulating files in python are as follows

Read file: r mode
Example demo 1:
f1 = open(file='D:\Python3.5-learn\module2\character3_file operation\staff_table.txt',mode='r',encoding='utf-8 ') # In r mode, open the file with the specified path and read it in utf-8

data = f.read() # Read the file, which means read everything

f.close() # close the file

Example demonstration 2:
f2 = open(file='D:\Python3.5-learn\module2\character3_file operation\staff_table.txt',mode='rb',encoding='utf-8') # with rb In binary mode, open the file with the specified path and read it in the way of utf-8

data = f.read() # Read the file, which means read everything

f2.close() # close the file

What is the difference between example 2 and example 1?

Answer: The encoding is not specified when the file is opened in example 2. Why is this? It is because the file is opened directly in rb mode. rb refers to binary mode. The data is read directly into the memory in bytes format. If you want the content, you need to manually decode it. Therefore, you do not need to specify the encoding during the file opening stage.

Loop through the files:

f = open(file='D:\Python3.5-learn\module2\character3_file operation\staff_table.txt',mode='r',encoding='utf-8') #

for line in f:

 print(line)

f.close()

Write file: w Mode
example demo 1:
f1 = open(file='D:\Python3.5-learn\module2\character3_file operation\staff_table.txt',mode='w',encoding='utf-8 ') # In w mode, open the file with the specified path, and write it in utf-8

data = f.write(".....") # write to the specified file

f.close() # close the file

Example demonstration 2:
f2 = open(file='D:\Python3.5-learn\module2\character3_file operation\staff_table.txt',mode='rb',encoding='utf-8') # with wb In binary mode, open the file with the specified path and read it in the way of utf-8

data = f.write(".....") # write to the specified file

f2.close() # close the file

Tip: When the file is operated in the way of w or wb, the original file will be emptied.

wb, when writing, you need to directly pass in 0100101 in a certain encoding, namely: byte type
w and encoding, when writing, you need to pass in a unicode string, and the unicode string will be converted to this code according to the encoding specified by the encoding. 010101010

Append file: a mode
f1 = open(file='D:\Python3.5-learn\module2\character3_file operation\staff_table.txt',mode='a',encoding='utf-8') # with w mode, open the file with the specified path, and write it in the way of utf-8

data = f.write("\n11,Kevin Chen,22,13151054603,Sales,2013-04-01") # Write the specified file

f1.close() # close the file

When the file is opened in "a" or "ab" mode, it can only be appended, that is: append the content to the end of the original content

When writing to the hard disk, it must be 0101010 in some kind of encoding, and you need to pay attention when opening:

ab, when writing, you need to directly pass in 0100101 in a certain encoding, that is: byte type
a and encoding, when writing, you need to pass in a unicode string, and the unicode string will be converted to the code according to the encoding specified by the encoding. 010101010

Read and write mode: r+
f1 = open("beiying2.txt", "r+",encoding="utf-8") # Open the file for reading and writing
data = f1.read() # Read the file
print(data)
f1.write("\n first line") # Append the file to the end of the file
f1.close() # Close the file

写读模式:w+
f1 = open("beiying.txt", "w+", encoding="utf-8")
data = f1.read()
print(data)
f1.write("line 1")
f1.write("\nline 2")
f1.write("\nline 3")
f1.write("\nline 4")
f1.close()

w+ will clear the file first, and then write new content. Compared with w mode, it only supports a read function, and can only read the new content that has been written.

Other ways of file operation:
def flush(self, *args, **kwargs): # real signature unknown
Force the file to be flushed from the memory buffer to the hard disk

def seek(self, *args, * kwargs): # real signature unknown Move
the cursor of the operation file to the specified position.
Note that the length of seek is calculated in bytes, and the length of bytes occupied by each character in the character encoding is different. .
For example, "Luffy Xuecheng" uses gbk to store 2 bytes per word, and utf-8 is 3 bytes, so when opening with gbk, seek(4) switches the cursor to "fly" and "learn" between the two words.
But if it is utf8, seek(4) will result in getting a part of the byte of the word Fei, and printing it will report an error, because when processing the remaining text, it is found that it cannot be processed with utf8, because the encoding is not correct. one byte less

def tell(self, *args, **kwargs): # real signature unknown
returns the current file operation cursor position

def truncate(self, *args, * kwargs): # real signature unknown If
the file is
truncated to the specified length, the specified length will be truncated from the beginning of the file. If the length is not specified, the content from the current position to the end of the file will be removed. .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324897698&siteId=291194637