Your problem is that you are trying to concatenate two byte
s and a str
. This is not possible in python3 because python3 makes a clear distinction between bytes and strings (a good thing given the somewhat fuzzy distinctions between str
and unicode
in python2). I think what you want is probably the following:
import marshal, imp
f=open('PYTHONSCRIPT','rb')
f.seek(28) # Skip the header, you have to know the header size beforehand
ob=marshal.load(f)
for i in range(0,len(ob)):
with open(str(i)+'.pyc','wb') as my_file:
my_file.write(imp.get_magic() + b'\0'*4 + marshal.dumps(ob[i]))
f.close()
with open(str(i)+'.pyc','wb') as my_file:
my_file.write(imp.get_magic() + b'\0'*4 + marshal.dumps(ob[i]))
The b
before the strings is a marker that tells python that the strings are byte
strings instead of str
strings.
Note also that I added with ... as ...:
which will make sure that your files get deterministically closed right away even in non-CPython implementations (PyPy, Jython, IronPython, etc).