Ai
1 Star 0 Fork 0

yihaohhh/Python学习

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
code25.py 558 Bytes
一键复制 编辑 原始数据 按行查看 历史
yihaohhh 提交于 2025-03-04 11:32 +08:00 . py学习
# 1. 使用read 来读取文件内容 只当读几个字符
f = open('d:/pythonWork/test.txt','r',encoding='utf8')
ret = f.read(10)
print(ret)
f.close()
# 2. 更常见的需求是按行来读取
# 最简单的办法就是for循环
f = open('d:/pythonWork/test.txt','r')
for i in f:
print(i,end = "")
f.close()
print("\n")
# 3. 还可以使用readlines 方法直接把整个文件所有内容全部都读出来,按照行组织到一个列表里面
f = open('d:/pythonWork/test.txt','r')
lines = f.readlines()
print(lines) # ['h1h\n', '2222']
f.close()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/liu-yihao-hhh/learning-python.git
git@gitee.com:liu-yihao-hhh/learning-python.git
liu-yihao-hhh
learning-python
Python学习
master

搜索帮助