Central African Python read plain text files

Central African Python read plain text files

If reading is a picture, audio, video and other (non-plain text files)
need to read and write binary way

- reading the text file
r r + w w + a a + === rt rt + wt wt + at at +

- Non-read plain text files
rb rb + wb wb + ab ab +

Example:
First a picture stored in the current directory, you can view this image
Here Insert Picture Description
Read operation:
code show as below:

#读取二进制文件内容
f1 = open('hello.jpg',mode='rb')
content = f1.read()
print(content)
f1.close()

Output:
Here Insert Picture Description

The copy operation:
code show as below:

#读取二进制文件内容
f1 = open('hello.jpg',mode='rb')
content = f1.read()
f1.close()
f2 = open('happy.jpg',mode='wb')
# 写入要复制的文件的内容
f2.write(content)
f2.close()

Output:
copy success, a new photo appears:
Here Insert Picture Description

Here Insert Picture Description

Published 59 original articles · won praise 6 · views 1321

Guess you like

Origin blog.csdn.net/weixin_45775963/article/details/103735771