python连接linux服务器读取txt文件_Python文件处理
1、創(chuàng)建文件:
f = file('myfile.txt','w')????#myfile 為文件名,w為寫權限。
f .write("hello world!")????#hello world! 為要寫入的文件的內(nèi)容。
f.close()????????????????????????????#文件打開后一定要記得關閉,因為當有其他程序再次打開該文件的時候會報錯。
文件的權限還有:
w+
r+
a+
wb,rb二進制形式讀寫文件
eg1:實時寫入
import time
f = file("f_test.txt",'w')
for i in range(15):
time.sleep(1)????#休眠一秒
f.write('The %s loops\n' % i)
f.flush()????????#每循環(huán)一次,講內(nèi)存中的數(shù)據(jù)寫入到硬盤中,
time.sleep(10) #循環(huán)結束,休眠10秒
f.close()
eg2:只有文件關閉或者程序結束之后,緩存在內(nèi)存中的東西才能寫入到磁盤中。
import time
f = file('f_test.txt','w')
for i in range(12):
f.write('The %s loops \n % i)
time.sleep(10)
f.close()
import time
import fileinput
for line in fileinput.input('f_test.txt',inplace=1):
line = line.replace('The 5 loops\n','Change me')
print line
總結
以上是生活随笔為你收集整理的python连接linux服务器读取txt文件_Python文件处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 交通银行是国企吗
- 下一篇: arm linux 识别新硬盘_嵌入式L