写"串QUOT;以原始二进制到一个文件中的Python [英] Write a "string" as raw binary into a file Python

查看:102
本文介绍了写"串QUOT;以原始二进制到一个文件中的Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写了一系列的文件进行测试,我从头开始建立的。数据有效载荷生成器的输出是字符串类型,而我努力让直接写入到文件中的字符串。

有效载荷生成器仅使用十六进制值,只是每次迭代增加一个字节。

我已经尝试了所有要么争先恐后字符串的写作,或写ASCII code字符串,在写功能,而不是字符串自身...

我想用一系列文件落得 - 具有相同文件名作为数据有效载荷(例如文件ff.txt包含字节为0xFF

 高清doMakeData(计数器):
    dataPayload =%X%计数器
    如果len(dataPayload)%2 == 1:
        dataPayload = STR('0')+ STR(dataPayload)
    文件名=路径+ STR(dataPayload)+。TXT
    返回dataPayload,文件名高清doFilenameMaker(计数器):
    计数器+ = 1
    返回柜台DEF了saveFile(dataPayload,文件名):
    #开放(文件名,W)为text_file:
          #text_file.write(%S%dataPayload)#此只写ASCII字符串
    F =文件(文件名,世行)
    dataPayload.write(F)#此也为字符串写入ASCII
    f.close()
    返回如果__name__ ==__main__:
    PATH =C:\\用户\\我\\桌面\\输出\\\\
    计数器= 0
    迭代器= 100
    而计数器<迭代器:
        计数器= doFilenameMaker(计数器)
        dataPayload,文件名= doMakeData(计数器)
        打印类型(dataPayload)
        saveFile的(dataPayload,文件名)


解决方案

要写出只是一个字节,使用 CHR(N)来得到一个包含字节整数 N

您code可以简化为:

 导入OS
PATH = R'C:\\用户\\我\\桌面\\输出'
在的xrange(100)计数器:
    开放(os.path.join(路径,'{:02X} .txt'.format(计数器)),世行)为f:
        f.write(CHR(计数器))

请注意使用原始字符串的路径。如果你有串在一个'\\ r'或'\\ n',他们将被视为一个回车换行或不使用原始字符串。

f.write 是写入文件的方法。 CHR(计数器)生成的字节。确保以二进制方式写WB以及

I'm trying to write a series of files for testing that I am building from scratch. The output of the data payload builder is of type string, and I'm struggling to get the string written directly to the file.

The payload builder only uses hex values, and simply adds a byte for each iteration.

The 'write' functions I have tried all either fall over the writing of strings, or write the ASCII code for the string, rather than the string its self...

I want to end up with a series of files - with the same filename as the data payload (e.g. file ff.txt contains the byte 0xff

def doMakeData(counter):
    dataPayload = "%X" %counter
    if len(dataPayload)%2==1:
        dataPayload = str('0') + str(dataPayload)
    fileName = path+str(dataPayload)+".txt"
    return dataPayload, fileName

def doFilenameMaker(counter):
    counter += 1
    return counter

def saveFile(dataPayload, fileName):
    # with open(fileName, "w") as text_file:
          # text_file.write("%s"%dataPayload)  #this just writes the ASCII for the string
    f = file(fileName, 'wb')
    dataPayload.write(f) #this also writes the ASCII for the string
    f.close()
    return

if __name__ == "__main__":
    path = "C:\Users\me\Desktop\output\\"
    counter = 0
    iterator = 100
    while counter < iterator:
        counter = doFilenameMaker(counter)
        dataPayload, fileName = doMakeData(counter)
        print type(dataPayload)
        saveFile(dataPayload, fileName)

解决方案

To write just a byte, use chr(n) to get a byte containing integer n.

Your code can be simplified to:

import os
path = r'C:\Users\me\Desktop\output'
for counter in xrange(100):
    with open(os.path.join(path,'{:02x}.txt'.format(counter)),'wb') as f:
        f.write(chr(counter))

Note use of raw string for the path. If you had a '\r' or '\n' in the string they would be treated as a carriage return or linefeed without using a raw string.

f.write is the method to write to a file. chr(counter) generates the byte. Make sure to write in binary mode 'wb' as well.

这篇关于写&QUOT;串QUOT;以原始二进制到一个文件中的Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆