久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python用字節(jié)處理文件實(shí)例講解

瀏覽:3日期:2022-06-22 15:55:46

1、可以在mode參數(shù)中添加’b’字符。所有適合文件對(duì)象的相同方法。然而,每種方法都希望并返回一個(gè)bytes對(duì)象。

>>> with open(`dog_breeds.txt`, ’rb’) as reader:>>> print(reader.readline())b’Pugn’

2、當(dāng)打開(kāi)文件并單獨(dú)閱讀這些字節(jié)時(shí),可以看到它確實(shí)是一個(gè)png文件:

>>> with open(’jack_russell.png’, ’rb’) as byte_reader:>>> print(byte_reader.read(1))>>> print(byte_reader.read(3))>>> print(byte_reader.read(2))>>> print(byte_reader.read(1))>>> print(byte_reader.read(1))b’x89’b’PNG’b’rn’b’x1a’b’n’

知識(shí)點(diǎn)擴(kuò)展:

讀取文件的字節(jié)流數(shù)據(jù),將其轉(zhuǎn)換為十六進(jìn)制數(shù)據(jù)

def read_file(): with open(’./flag.zip’,’rb’) as file_byte:file_hex = file_byte.read().hex()print(file_hex)write_file(file_hex)def write_file(file_hex): with open(’new.txt’,’w’) as new_file:new_file.write(file_hex)if __name__ == ’__main__’: read_file()

讀取文件的字節(jié)流數(shù)據(jù),將其編碼為base64并輸出

import base64def read_file(): with open(’./flag.zip’,’rb’) as file_byte:file_base64 = base64.b64encode(file_byte.read())print(file_base64)if __name__ == ’__main__’: read_file()

將十六進(jìn)制文件轉(zhuǎn)化為字節(jié)流文件寫入

import structa = open('str.txt','r')#十六進(jìn)制數(shù)據(jù)文件lines = a.read()res = [lines[i:i+2] for i in range(0,len(lines),2)]with open('xxx.xxx','wb') as f:for i in res:s = struct.pack(’B’,int(i,16))f.write(s)

以上就是python用字節(jié)處理文件實(shí)例講解的詳細(xì)內(nèi)容,更多關(guān)于python使用字節(jié)處理文件的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 雅安市| 肥城市| 澎湖县| 民县| 南宁市| 黑河市| 甘谷县| 玉门市| 黄山市| 长丰县| 泰顺县| 陈巴尔虎旗| 包头市| 武夷山市| 永吉县| 都兰县| 安新县| 丹江口市| 通州区| 靖江市| 库伦旗| 连江县| 岐山县| 朝阳区| 兰州市| 黄浦区| 九寨沟县| 理塘县| 台湾省| 红原县| 通州市| 黔西县| 合作市| 长岛县| 拉萨市| 卓尼县| 会宁县| 盐池县| 水富县| 阿城市| 丹凤县|