r/programminghelp • u/chris6251994 • 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()
View all comments
Show parent comment
1
u/chris6251994 Mar 27 '25 edited Mar 27 '25
I'm just doing it to do it. But no. The goal is to have a python file that is kind of like a zip file without the compression part. So running the python file will save the read bytes and store them in a new python and delete the files in the directory when done. Then when ran it will re-write the files kind of like unzipping a zip file. I have a working version when values are in hex format, but the size is double that of original data. This is just me coming up with random things to do because I am exploring programming as my hobby and maybe a career one day. It's fun to come up with ideas and do them just cause.