學(xué)習(xí)了:https://www.cnblogs.com/scse11061160/p/5605190.html
file = open("sample.txt") for line in file:
? ? pass# do something
file.close()
學(xué)習(xí)了:https://blog.csdn.net/ysdaniel/article/details/7970883
去除換行符
for line in file.readlines():?
? ? line=line.strip('\n')
學(xué)習(xí)了:https://www.cnblogs.com/feiyueNotes/p/7897064.html
mobile = Method.createPhone()
file = r'D:\test.txt'
with open(file, 'a+') as f:
? ? f.write(mobile+'\n')? #加\n換行顯示
'r':讀
'w':寫(xiě)
'a':追加
'r+' == r+w(可讀可寫(xiě),文件若不存在就報(bào)錯(cuò)(IOError))
'w+' == w+r(可讀可寫(xiě),文件若不存在就創(chuàng)建)
'a+' ==a+r(可追加可寫(xiě),文件若不存在就創(chuàng)建)
對(duì)應(yīng)的,如果是二進(jìn)制文件,就都加一個(gè)b就好啦:
'rb' 'wb' 'ab' 'rb+' 'wb+' 'ab+'