首页 / PYTHON / python 读取文件函数
python 读取文件函数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python 读取文件函数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1242字,纯文字阅读大概需要2分钟。
内容图文
![python 读取文件函数](/upload/InfoBanner/zyjiaocheng/636/783587a5359d43948ba7acfe5fbbc757.jpg)
python 读取文件函数
觉得有用的话,欢迎一起讨论相互学习~
感谢莫烦老师
详情
读取文件内容 file.read()
- 使用 file.read() 能够读取到文本的所有内容.
file= open('my file.txt','r')
content=file.read()
print(content)
""""
This is my first test.
This is the second line.
This the third line.
This is appended file.
""""
按行读取 file.readline()
- 如果想在文本中一行行的读取文本, 可以使用 file.readline(), file.readline() 读取的内容和你使用的次数有关, 使用第二次的时候, 读取到的是文本的第二行, 并可以以此类推:
file= open('my file.txt','r')
content=file.readline() # 读取第一行
print(content)
""""
This is my first test.
""""
second_read_time=file.readline() # 读取第二行
print(second_read_time)
"""
This is the second line.
"""
读取所有行 file.readlines()
- 如果想要读取所有行, 并可以使用像 for 一样的迭代器迭代这些行结果, 我们可以使用 file.readlines(), 将每一行的结果存储在 list 中, 方便以后迭代.
file= open('my file.txt','r')
content=file.readlines() # python_list 形式
print(content)
""""
['This is my first test.\n', 'This is the second line.\n', 'This the third line.\n', 'This is appended file.']
""""
# 之后如果使用 for 来迭代输出:
for item in content:
print(item)
"""
This is my first test.
This is the second line.
This the third line.
This is appended file.
"""
内容总结
以上是互联网集市为您收集整理的python 读取文件函数全部内容,希望文章能够帮你解决python 读取文件函数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。