What is the use of binaries in Python
This article mainly introduces what is the use of binary files in Python. The introduction in this article is very detailed and has certain reference value. Interested friends must read it!
1. binary read mode
rt Read text file (default)
RB reads binary files
file_name = "C:/Users/cheng/Desktop/can.jmx"with open(file_name, 'rt', ) as can: print(can.read())
execution result
rb reads the pattern file_name = "C:/Users/cheng/Desktop/can.jmx"with open(file_name, 'rb', ) as can: print(can.read())
execution result
Read 100 bytes
file_name = "C:/Users/cheng/Desktop/can.jmx"with open(file_name, 'rb', ) as can: #Read text files, size is in characters #Read binary file, size is in bytes print(can.read(100))
execution result
File_name = "C:/Users/cheng/Desktop/can.jmx"with open(file_name, 'rb',) as can: #Define a new file new_name = 'to.jmx' with open(new_name, 'wb') as cheng: #Define the size of each read cc = 1024 * 100 while True: #Read data from existing objects content = can.read(cc) #Content read complete, end loop if not content: break #Write the read data to the new object cheng.write(content)
Implementation results:
The above is "Python binary file what to use" all the content of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!