( It is only in Python 3 ! )
( The commands to operate this file are at Linux ! :
I want to read a file, but it has to be through stdin ( In linux, with "< filename" ) !
And I want to write the same file to another file with a different name ( In linux, with "> newfilename" ).
)
Hello.
I have written the following code. The code should read bytes (!) from a binary file, and write them to another file.
It's like copy the binary file, but get it through stdin, and write it to stdout. It has to be through stdin / stdout.
testFile2.py:
import sys
def make_streams_binary():
sys.stdin = sys.stdin.detach()
#sys.stdin = codecs.getwriter("utf-8")(sys.stdin.detach())
#sys.stdout = sys.stdout.detach()
#sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
make_streams_binary()
while 1:
try:
file = input()
except KeyboardInterrupt:
#print("ctrl-c")
break
except EOFError:
break
print(file)
And to test the code:
cmd: python3 testFile2.py < Viper2.jpg > test_output.jpg
Printing the files to the screen to see the difference:
cmd : python3 testFile2.py < Viper2.jpg
b'\x1c\xa9\xacw\xa9AM6\x8e\xc6"\xb9\x13\xba \xe4\xfa\x00G"\xaf\xc7w\x89\xaa\x9f?\x1d\xba\x03?\x08|c\xb3\xde_\xe8\r\x8d\xbe0\xfd\xaf\xb65\xdc\x8c9\x85\x91\x8bVl[J\x0bh9\xe3\xa3"\x12Lq5+\x00w8Cp}f\x84\x8a\xe6$\x86+_\xdd\x15R\xe5|\x9f\xde\xbd\x05Ix\xafp_\\\xf2\xd7{l\x05\xdc\xf0\xa6\xc6\xc51\x9b\\r\xda\xe3+\xc7\x9d\xed\xef18\x93kK\x07\xee\xd1O\xd9\xc8\xf0\x88\xd0G\xee\xc0F\xb9\x8e\x8e\xd7\x91\xaa\xc2Gj\xb95\xe3\x7f\xd2\x05u\xbc\xb5\x1c}\x9f\x96\xef\xc9:\xfa\xc0\x12\xa5\xc4\x93\x87eTo\x9121c\xcc,s4\xe6\x0c\x861\xccF\x85\x08\x12\x05\xa8\xd20\x8cr\xa2"\xf4\x1dN\x18\xf8Mb\x1cg\xd5s\xb6un+\x93\x13(\x8d\x8d${\xb9[\x01DCc\xc4\xb8\xb1\x0cZ\xea\xe8\x11\x07\xf9c\xca\x95\x10\xa9.S\x9e\xa4 C*0S\xca\xe9\x0fzz$\xe8\x03|\x95\xe2\xed\x86\xd4\x9eM\x91\xaa\xf2\xa8\xb49\x8f\xda\xdfM%\xb6\xb1VeFI\\\xe5{\x9d]c\x19\x1c\xd7\x10=\xc8Ea\x06\xe6\x94.+\xd5\xaa\xady\x06@\x04\x9efs\x97\x8eX\xa1\xb8\xd8\xff\x00\x0c\xac\xff\x00"\xcb\xab\xe0\xc4\xac\xc2n1{\x10\xddc\xf7\xa80\x0cD$\xebg(}\x8f\x91\xec{\xd5e\x0cN{<\xbeTW\xf7N\x80\x81\xa1xm\xb1\xef\xb2w\xec\xaeJ\xca\x85_\r\xf9\x11r\xd8\xda\xf2\x9aR\xcca,\x9eD \xa6YNV\xb3\xdd\x94\x08\xc14 \x13\x19\x1c>\xdcN\xfdw\xb0dcA\xe8\x0b\xfa\x7f\xb5\xe87\xea4\x8f\xe4\'\xf8\x7f\xd7A\'\xac\xe8?\xff\xd9'
cmd : python3 testFile2.py < test_output.jpg
b'b\'\\x1c\\xa9\\xacw\\xa9AM6\\x8e\\xc6"\\xb9\\x13\\xba \\xe4\\xfa\\x00G"\\xaf\\xc7w\\x89\\xaa\\x9f?\\x1d\\xba\\x03?\\x08|c\\xb3\\xde_\\xe8\\r\\x8d\\xbe0\\xfd\\xaf\\xb65\\xdc\\x8c9\\x85\\x91\\x8bVl[J\\x0bh9\\xe3\\xa3"\\x12Lq5+\\x00w8Cp}f\\x84\\x8a\\xe6$\\x86+_\\xdd\\x15R\\xe5|\\x9f\\xde\\xbd\\x05Ix\\xafp_\\\\\\xf2\\xd7{l\\x05\\xdc\\xf0\\xa6\\xc6\\xc51\\x9b\\\\r\\xda\\xe3+\\xc7\\x9d\\xed\\xef18\\x93kK\\x07\\xee\\xd1O\\xd9\\xc8\\xf0\\x88\\xd0G\\xee\\xc0F\\xb9\\x8e\\x8e\\xd7\\x91\\xaa\\xc2Gj\\xb95\\xe3\\x7f\\xd2\\x05u\\xbc\\xb5\\x1c}\\x9f\\x96\\xef\\xc9:\\xfa\\xc0\\x12\\xa5\\xc4\\x93\\x87eTo\\x9121c\\xcc,s4\\xe6\\x0c\\x861\\xccF\\x85\\x08\\x12\\x05\\xa8\\xd20\\x8cr\\xa2"\\xf4\\x1dN\\x18\\xf8Mb\\x1cg\\xd5s\\xb6un+\\x93\\x13(\\x8d\\x8d${\\xb9[\\x01DCc\\xc4\\xb8\\xb1\\x0cZ\\xea\\xe8\\x11\\x07\\xf9c\\xca\\x95\\x10\\xa9.S\\x9e\\xa4 C*0S\\xca\\xe9\\x0fzz$\\xe8\\x03|\\x95\\xe2\\xed\\x86\\xd4\\x9eM\\x91\\xaa\\xf2\\xa8\\xb49\\x8f\\xda\\xdfM%\\xb6\\xb1VeFI\\\\\\xe5{\\x9d]c\\x19\\x1c\\xd7\\x10=\\xc8Ea\\x06\\xe6\\x94.+\\xd5\\xaa\\xady\\x06@\\x04\\x9efs\\x97\\x8eX\\xa1\\xb8\\xd8\\xff\\x00\\x0c\\xac\\xff\\x00"\\xcb\\xab\\xe0\\xc4\\xac\\xc2n1{\\x10\\xddc\\xf7\\xa80\\x0cD$\\xebg(}\\x8f\\x91\\xec{\\xd5e\\x0cN{<\\xbeTW\\xf7N\\x80\\x81\\xa1xm\\xb1\\xef\\xb2w\\xec\\xaeJ\\xca\\x85_\\r\\xf9\\x11r\\xd8\\xda\\xf2\\x9aR\\xcca,\\x9eD \\xa6YNV\\xb3\\xdd\\x94\\x08\\xc14 \\x13\\x19\\x1c>\\xdcN\\xfdw\\xb0dcA\\xe8\\x0b\\xfa\\x7f\\xb5\\xe87\\xea4\\x8f\\xe4\\\'\\xf8\\x7f\\xd7A\\\'\\xac\\xe8?\\xff\\xd9\''
It is almost the same file. The original takes 990 bytes, and the output file is 1219 bytes.
As you can see the difference between the files: At the orig file, there is one '\' every few chars, and the output has two '\'.
I tried to run the third line at make_streams_binary() and it did weird stuff.
Can you please tell me how to change my code in order to make it work ?
Thanks.