* 模式字符
模式字符 |
表示的操作 |
r |
只讀(默認) |
w |
可寫,會清除文件內(nèi)容 |
a |
附加數(shù)據(jù) |
b |
二進制模式 |
x |
新建一個文件撮弧,可寫 |
+ |
打開文件直接更新 |
t |
文本模式,默認 |
* 文件操作
文件操作 |
功能描敘 |
file.read([n]) |
將文件讀入字符串中 |
file.readline([n]) |
讀入文件的一行到字符串中 |
file.readlines() |
將整個文件讀入到列表中 |
file.write(s) |
寫字符串到文件中 |
file.writelines(lines) |
向文件中寫入一個行數(shù)據(jù)列表 |
file.close() |
關(guān)閉打開的文件 |
myfile = open('myfile.txt','w')
myfile.write('hello text file\n')
myfile.write('goodbye text file\n')
18
myfile = open('myfile.txt')
myfile.readline()
myfile.readlines()
['goodbye text file\n']
open('myfile.txt').read() #以字符串的形式展示
'hello text file\ngoodbye text file\n'
print(open('myfile.txt').read()) #print格式輸出
hello text file
goodbye text file
for line in open('myfile.txt'): #迭代器遍歷文件每一行
print(line)
hello text file
goodbye text file
file = open('myfile.txt','r')
while True: #while語句循環(huán)遍歷文件并輸出每一行
line = file.readline()
print(line)
if not line:
break
hello text file
goodbye text file
未完待續(xù)姚糊。贿衍。。救恨。贸辈。。肠槽。擎淤。。秸仙。嘴拢。。寂纪。席吴。。捞蛋。孝冒。。拟杉。庄涡。。搬设。
author:jack rose
date:2018.10.23
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者