文件的类型

Python中的文件类型包括:文本文件和二进制文件两种类型,但本质上都是以二进制形式存储的。

文本文件:由单一特定编码组成的文件,如UTF-8编码,包括 .txt文件、 .py文件等等

二进制文件:直接由比特0和1组成,没有统一字符编码,包括 .png文件、.avi文件等

举例如下:

programming.txt文件保存:“我相信明天会更好!!!”

 1 >>> filename = "D:\Software\Sublime Text Build 3176 x64\Program\My Data Structure\programming.txt"
 2 
 3 >>> with open(filename,'rt') as file_object:     #文本形式打打开文件
 4     print(file_object.readline())
 5 
 6 我相信明天会更好!!!
 7 
 8 >>> with open(filename,'rb') as file_object:  #二进制形式打开文件
 9     print(file_object.readline())
10     
11 b'xcexd2xcfxe0xd0xc5xc3xf7xccxecxbbxe1xb8xfcxbaxc3xa3xa1xa3xa1xa3xa1'
原文地址:https://www.cnblogs.com/fsy12604/p/9938362.html