python 读取文件函数
2020-04-05 18:29
1331 查看
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学习日记】函数列表 动态创建函数 文件读取 迭代器
- python之文件读取seek(),tell()函数
- python核心编程学习笔记-2016-08-02-01-读取文件的函数中的文件指针问题
- Python 文件读取相关操作函数
- 【python】如何批量读取文件夹的所有文件数据,os模块与open函数结合使用实例
- 解决python中os.listdir()函数读取文件夹下文件的乱序和排序问题
- python中读取文件函数read()、readline()、readlines()的区别
- Python读取多个txt文件并进行保存
- python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multibyte sequence
- python 读取文件
- python读取zip压缩文件
- C语言自己写得到文件大小的函数已及Python
- 查看和显示.nii文件(python读取并显示NIFIT格式文件)
- Python3学习笔记05-字典、文件输入输出、模块、函数参数传递
- python中读取txt文件,windows下么有毛病,到Linux下总是只有最后一行有效,怎么解决?
- 机器学习之python读取CSV文件
- Python读取csv文件分隔符设置方法
- 【函数调用】上传文件并读取文件内容至内表
- Python 读取WAV音频文件 画频谱