r/programminghelp • • Mar 26 '25

Python Problem with my file program handling bytes properly.

Hello I have created a program called 'file2py.py' . It worked by storing read bytes to a variable and converting to hex data. Then it would write a new python file with the code to restore said files as their hex data was stored in the python file itself. running would restore all files in directory and sub directories. The problem I noticed was the python file itself would be slightly bigger than double the original data which I should have accounted for but it didn't cross my mind. So I decided to change to program to just write the raw byte data but with the raw data I seem to be having issues. When I have the new python file created the variable will fail as it will not take the string because of the raw bytes structure. I've been trying to figure it out for days but I am just a programmer by hobby and have no deep understanding of everything. Maybe one day lol. 1st image gives me a string literal error. The second one I tried using triple quotations to ignore line breaks and it gives me a utf-8 encoding error. If I want to use raw bytes am I going to have to find out the encoding for every different file type first? Is there even a way to resolve this issue? This is just a small test file I am using before trying to incorporate it into main.

Code 1:

with open('./2.pdf', "rb") as f:
    data = f.read()
    f.close()


with open('file.py', 'a') as f:
    f.write('data = "')
    f.close()


with open('file.py', 'ab') as f:
    f.write(data)
    f.close


with open('file.py', 'a') as f:
    f.write('"\n\nwith open("newfile.pdf", "wb") as f:\n   f.write(data)\n   f.close()')
    f.close()

Code: 2

with open('./2.pdf', "rb") as f:
    data = f.read()
    f.close()


with open('file.py', 'a') as f:
    f.write('data = """')
    f.close()


with open('file.py', 'ab') as f:
    f.write(data)
    f.close


with open('file.py', 'a') as f:
    f.write('"""\n\nwith open("newfile.pdf", "wb") as f:\n   f.write(data)\n   f.close()')
    f.close()
1 Upvotes

4 comments sorted by

View all comments

Show parent comment

2

u/Lewinator56 Mar 27 '25

Hex isnt a format, it's just a representation of data, I assume what you mean is you're writing the hex string to a text file. It's twice the size because each ASCII character is 1 byte, whereas each character in a hexadecimal representation is 4 bits (but you're writing text to your archive, not pure binary, so the characters can be interpreted as hex, but they aren't stored as binary representing the data in the file).

It seems though that you have a segmenting method figured out if your system writing the plain text back works. You should be able to simply write the entire byte array to your file by writing in wb mode. You need to make sure that you're writing bytes and NOT text. You won't be able to feed the write function a string like you used for hex. Cast your array first to a bytearray: ba = bytearray(my_array), then cast that to a bytes structure and write it: my_file.write(bytes(ba))