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
Read operation:
code show as below:
#读取二进制文件内容
f1 = open('hello.jpg',mode='rb')
content = f1.read()
print(content)
f1.close()
Output:
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: