python 檔案操作執行個體——待完善

來源:互聯網
上載者:User

標籤:print   run   day   int   二進位檔案   lines   格式   網路傳輸   避免   

#w寫入模式,r讀模式,a追加,r+讀寫,w+寫讀,a+追加讀,rb二級制格式讀檔案,wb寫二級制檔案
‘‘‘
f = open(‘yesterday‘,‘a+‘,encoding=‘utf-8‘) #檔案控制代碼(檔案記憶體對象):
f = open(‘yesterday‘,‘r+‘,encoding=‘utf-8‘)
f = open(‘yesterday‘,‘w+‘,encoding=‘utf-8‘)
f = open(‘yesterday‘,‘rb‘)#檔案控制代碼,二進位檔案,兩種情況使用:網路傳輸,讀二進位檔案,避免損害檔案
f = open(‘yesterday‘,‘wb‘)#寫為二進位檔案
#data = f.read()
#print(data)
f.write(‘\n我愛北京天安門。。。\n‘)
f.write(‘天安門上太陽升‘)
data = f.read()
print(data)
f.close()
‘‘‘
‘‘‘‘
f = open(‘yesterday‘,‘r+‘,encoding=‘utf-8‘)
print(f.readline())
print(f.readline())
print(f.readline())
print(f.tell())
f.write(‘-------------------------------------------------‘)

print(f.readline())
f.close()
‘‘‘
f = open(‘yesterday‘,‘wb‘)

f = open(‘yesterday‘,‘rb‘)
#f.write("hello binary\n".encode()) #存二級制則需要轉換為二級制,不然報錯
print(f.readline())
f.close()


‘‘‘
print(f.tell()) #列印當前的位置
print(f.readline())
print(f.readline())
print(f.readline())
print(f.tell())
f.seek(10) #回到的地方
print(f.readline())
print(f.encoding) #返回編碼格式
print(f.fileno()) #檔案控制代碼編號
print(f.seekable())#判斷游標是否可以移動
print(f.readable())#檔案是否可讀
print(f.writable())#判斷檔案是否可以寫
‘‘‘

‘‘‘
f.write(‘hello 1\n‘)
print(f.buffer)
print(f.flush()) #重新整理到硬碟
f.seek(10)
f.truncate(20)#不寫就是清空,從頭開始截斷
‘‘‘

#for i in range(5):
# print(f.readline())
#print(f.readline())
#for line in f.readline():
#print(f.readlines())
#low
‘‘‘
for index,line in enumerate(f.readlines()):#f.readlines()適合小檔案
if index==9:
print(‘-----------我是第9行-------------‘)
continue
print(line.strip())
‘‘‘
#high bige 一行一行讀,且記憶體只保留一樣
‘‘‘‘
count = 0
for line in f:
if count ==9:
print(‘-----------我是第9行-------------‘)
count += 1
continue
print(line.strip())
count +=1
‘‘‘


python 檔案操作執行個體——待完善

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.