“”"
python文件操作的几种方法:
读:r,rb,r+,r+b
写:w,wb,w+,w+b
加:a,ab,a+,a+b
tell():获取光标的位置
seek:调整光标的位置
flush:强制刷新
“”"
f = open(“中国.txt”,encoding = “utf-8”,mode = “r”)
content = f.read()
print(content)
f.close()
read(n)按照字符去读
f = open(“中国.txt”,encoding = “utf-8”,mode = “r”)
content = f.read(4)
print(content)
f.close()
readline()一行一行读
f = open(“中国.txt”,encoding = “utf-8”,mode = “r”)
content = f.readline()
print(content)
f.close()
readlines()返回一个列表,列表的每个元素就是每行
f = open(“中国.txt”,encoding = “utf-8”,mode = “r”)
content = f.readlines()
print(content)
f.close()
for读取
f = open(“中国.txt”,encoding = “utf-8”,mode = “r”)
for line in f:
print(line)
f.close(
#rb操作非文本的文件