Reputation: 466
i have a python source code (with name source.py
) that display some data like
b'testdata \xe3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1f\x00\x00\x00@\x00\......
on terminal.
i want to get this informations and save them on a variable, so i wrote this code :
import subprocess
batcmd= 'python3.8 source.py'
result = subprocess.check_output(batcmd, shell=True)
print (result)
output:
b'testdata \\xe3\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x1f\\x00\\x00\\x00@\\x00\\......
problem is any \
will duplicate 2 times!
how i can solve this issue ?
Notes :
1 - i can't edit source.py! i can't Manipulation it, we can only read data from terminal.
2 - i recently used
_w_long_
and then i edited my post and algorithm , So if you see it in the comments, it's because and ignore it.3- Stored data must remain in bytes
UPDATE:
i also tried :
from contextlib import redirect_stdout
import io,os
res = os.popen("decompyle3 start.py.pyc").read()
print (res)
print (res.encode('latin1'))
in this code : print(res)
, will print this data with type string in output :
"b'\xe3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1f\x00\x00\x00@\x00\x00\x00sV\x00\x00\x00e\x00e\x01d\x00d\x01\x84\x00d\x02d\x03d\x03d\x04d\x05d\x06d\x07d\x08d\td\x03d\x03d\nd\x0bd\x05d\x0cd\x08d\rd\x0ed\x0cd\x0fd\..."
so i want convert this string to Byte without any changing, i want this output :
b'\xe3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1f\x00\x00\x00@\x00\x00\x00sV\x00\x00\x00e\x00e\x01d\x00d\x01\x84\x00d\x02d\x03d\x03d\x04d\x05d\x06d\x07d\x08d\td\x03d\x03d\nd\x0bd\x05d\x0cd\x08d\rd\x0ed\x0cd\x0fd\...
i tried :
print (res.encode('latin1'))
that printed :
b\'\\xe3\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x1f\\x00\\x00\\x00@\\x00\\x00\\x00sV\\x00\\x00\\x00e\\x00e\\x01d\\x00d\\x01\\x84\\x00d\\x02d\\x03d\\x03d\\x04d\\x05d\\x06d\\x07d\\x08d\\td\\x03d\\x03d\\nd\\x0bd\\x05d\\x0cd\\x08d\\rd\\x0ed\\x0cd\\x0fd...
As we see any \
duplicated !
how solve this problem?
Upvotes: 0
Views: 205
Reputation: 21
from contextlib import redirect_stdout
import io,os
with io.StringIO() as buf, redirect_stdout(buf):
os.system("python3.8 source.py")
res = buf.getvalue()
print(res)
Upvotes: 1