1、文件操作
1.1 读文件
- 读文本文件
##打开文件
- 路径:
相对路径:‘info.txt’
绝对路径: ‘/Users/pengfei/day09/info.txt’
# 打开文件
file_object = open('info.txt',mode = 'rb')
# 读取文件
data = file_object.read()
# 关闭文件
file_object.close()
text = data.decode('utf-8')
print(text)
- 读图片等非文本内容文件
file_object = open('al.png',mode = 'rb')
data = file_object.read()
file_object.close()
本文介绍如何使用Python进行文件操作,包括文本文件和非文本文件的读取过程。详细讲解了文件打开、读取及关闭的方法,并提供了具体的代码示例。

被折叠的 条评论
为什么被折叠?



