Python 官方文档:入门教程 => 点击学习
python 读写文件磁盘上的文件,是通过调用操作系统系统的接口来实现的,通过操作系统提供的接口,来读取或者写入文件,Python 读取文件的步骤如下# 使用 open 1、打开文件f1 = open('t1.txt', 'r',en
python 读写文件磁盘上的文件,是通过调用操作系统系统的接口来实现的,通过操作系统提供的接口,来读取或者写入文件,Python 读取文件的步骤如下
# 使用 open
1、打开文件
f1 = open('t1.txt', 'r',encoding='utf-8')
2、读取文件(文件存在的话,不存在的话,会报FileNotFoundError 错误)
data = f1.read()
print(data)
hello world
hello world
hello world
hello world
hello world
假设文件不存在的话,比如打开文件 t2.txt
3、关闭文件
f1.close()
使用 with open 方法,当文件读写完成后,会自动帮我们调用 close 方法
with open('t1.txt', 'r') as f1:
print(f1.read())
输出:
hello world
hello world
hello world
hello world
hello world
--结束END--
本文标题: Python 文件操作 open 和
本文链接: https://www.lsjlt.com/news/191490.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0