新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python操作文件模式的介紹
1、只讀模式(只讀數(shù)據(jù)r)。

我們提供的服務(wù)有:成都網(wǎng)站設(shè)計、成都做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、平安ssl等。為上千余家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的平安網(wǎng)站制作公司
f = open('my_file.txt', 'rb')
f_bytes = f.read()
print(f_bytes)
print(f_bytes.decode('utf-8'))
f.close()
2、只寫模式(重寫數(shù)據(jù)w),以二進(jìn)制的方式寫入數(shù)據(jù)或只寫模式追加數(shù)據(jù)。
write_file = open('demo1.jpg', mode='wb')
read_file = open('demo.jpg', mode='rb')
write_file.write(read_file.read())
read_file.close()
write_file.close()
3、讀寫模式(追加數(shù)據(jù)a),如果不想原有數(shù)據(jù)被覆蓋可以使用追加寫入數(shù)據(jù)的方式。
read_write_file = open('append_file.txt', mode='r+', encoding='utf-8')
print(read_write_file.read())
read_write_file.write('\n努力向上,天天向上')
print(read_write_file.read())
read_write_file.close()
以上就是python操作文件模式的介紹,希望對大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)Python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
網(wǎng)頁標(biāo)題:創(chuàng)新互聯(lián)Python教程:python操作文件模式的介紹
文章URL:http://m.5511xx.com/article/djcopeh.html


咨詢
建站咨詢
