【Python】文件操作,read(), readline(), readlines()
2014-05-22 16:57
651 查看
To open a file:
open(filename, mode)
mode:
‘r’ to open for reading
'w' to open for writing
'a' to open for appending(adding to what is already in the file)
>>> file = 'C:\\Users\document.txt' 双斜杠避免被解释为转义字符
>>> filename = open(file, 'r')
>>> line = filename.readline()
>>> while line != '':
print(line)
line = filename.readline()
>>> filename.close()
when you want to process every line in a file use the for line in file approach:
for line in file:
process line
file.read(): print the whole document as a string
list = file.readlines():自动将文件分析成一个行的列表,这个列表可由python的for...in...进行处理 如,for line in file.readlines()
readline():read and return the next line from the file, including the newline character(if it exists)
return the empty string if there are no more lines in the file,每次只读取一行
open(filename, mode)
mode:
‘r’ to open for reading
'w' to open for writing
'a' to open for appending(adding to what is already in the file)
>>> file = 'C:\\Users\document.txt' 双斜杠避免被解释为转义字符
>>> filename = open(file, 'r')
>>> line = filename.readline()
>>> while line != '':
print(line)
line = filename.readline()
>>> filename.close()
when you want to process every line in a file use the for line in file approach:
for line in file:
process line
file.read(): print the whole document as a string
list = file.readlines():自动将文件分析成一个行的列表,这个列表可由python的for...in...进行处理 如,for line in file.readlines()
readline():read and return the next line from the file, including the newline character(if it exists)
return the empty string if there are no more lines in the file,每次只读取一行
相关文章推荐
- [python]文件操作read&readline&readlines
- python文件操作,读取,read, readline, readlines
- python 点滴记录8:文件操作read、readline与readlines
- Python中三个读文件函数read、readline和readlines的简要区别
- 重学Python - Day 05 - python基础 -> python的文件操作:r、w、a、r+、a+ 、readline、readlines 、flush等常用的文件方法
- 【体会Python之美】【文件】file,open,read,readline,readlines,write,close
- python读操作:read(),readline(),readlines() 的区别
- Python中文件读取方法read(),readline(),readlines()的区别
- python读文件的三个方法read()、readline()、readlines()详解
- python 3-5-1 关于文件的操作-open/read/readlines/seek/write/writelines
- python读文件read()、readline()、readlines()三个方法的区别
- 实例说明Python文件读写中的read(), readline(),readlines()的区别
- python中读取文件函数read()、readline()、readlines()的区别
- Python文本操作之read() readline() readlines()
- python:大文件迭代器,GB级别,read(),readline(),readlines()以及语法糖with open()
- python 读取文件f.read()、f.readline()和f.readlines()
- python中的三个读read(),readline()和readlines()
- python中的三个读read(),readline()和readlines()
- python中read() readline()以及readlines()区别
- python中read()、readline()和readlines()的区别