1

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)

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thats so much easier. Thank you. I will pick through your code so I can understand whats going on here. I really appreciate your help.
1
dataPayload.write(f) # this fails "AttributeError: 'str' object has no attribute 'write'

Of course it does. You don't write to strings; you write to files:

f.write(dataPayload)

That is to say, write() is a method of file objects, not a method of string objects.

You got this right in the commented-out code just above it; not sure why you switched it around here...

3 Comments

Ah, thats a foolish mistake, thank you for catching that. However, that still writes the ASCII for the string, not the string as binary (I peeked at the file 0A.txt in a hex editor and can see 0x30 0x41, not 0x0A as I want... )
Well, that's because you're formatting them to hexadecimal. Don't do that, just convert the numbers using chr() and add them to the string. Or use a bytearray() -- it's mutable, so is a little more efficient.
OK. I think I understand what you mean. I will go and have a rummage on those methods. Thanks for your time. I appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.