我正在尝试编写一系列用于测试的文件,这些文件是我从头开始构建的。数据负载构建器的输出是字符串类型,我正在努力将字符串直接写入文件。
有效负载构建器仅使用十六进制值,并且只是为每次迭代添加一个字节。
我尝试过的“写入”函数要么落在字符串的写入上,要么为字符串编写 ASCII 码,而不是字符串本身......
我想以一系列文件结束 - 文件名与数据有效负载相同(例如文件 ff.txt 包含字节 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)