( 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.

It appears the the backslash is being escaped by Python. Try repr(the_string) as output and see if it helps.

Here is a working version

#!/usr/bin/python3
import sys

BUFSIZE = 4096

def make_streams_binary():
    sys.stdin = sys.stdin.detach()
    sys.stdout = sys.stdout.detach()

def main():
    make_streams_binary()
    while True:
        data = sys.stdin.read(BUFSIZE)
        if not data:
            break
        sys.stdout.write(data)

if __name__ == "__main__":
    main()

Test in a console

$ chmod +x streams.py
$ streams.py < dsc01390.jpg > tmp.jpg
$ diff dsc01390.jpg tmp.jpg
$

Gribouillis :
Thanks !
That's exactly what I need ( I think ).
When I do it with "streams.py < dsc01390.jpg > tmp.jpg", I mean from a file to a file,
it copy it exactly, as I need.
Although when I just want to see the file printed to the screen, when after I write the previous line, I write "streams.py < tmp.jpg", I see junk, and not bytes as my first attemp ("b'b\'\\x1c\\xa9...").

* I did not write "chmod +x streams.py" command, I suppose it is not neccessary ?

Gribouillis :
Thanks !
That's exactly what I need ( I think ).
When I do it with "streams.py < dsc01390.jpg > tmp.jpg", I mean from a file to a file,
it copy it exactly, as I need.
Although when I just want to see the file printed to the screen, when after I write the previous line, I write "streams.py < tmp.jpg", I see junk, and not bytes as my first attemp ("b'b\'\\x1c\\xa9...").

* I did not write "chmod +x streams.py" command, I suppose it is not neccessary ?

You should not print binary data to the console. For example cat tmp.jpg prints junk on the screen. Also printing binary data may corrupt your terminal, and also try to execute unwanted commands. The "chmod +x streams.py" makes streams.py executable. It allows you to invoke "streams.py" instead of "python3 streams.py", because I added a shebang line in the program.

Thanks again, I see your point with "cat" command.
( The reason I need to things as I said is because a specific demand from this program to act like I described... )

Thanks again, I see your point with "cat" command.
( The reason I need to things as I said is because a specific demand from this program to act like I described... )

Using optparse, you can easily add an option -x to your script to output the content
in hexadecimal code

#!/usr/bin/python3
import sys
from optparse import OptionParser
from binascii import hexlify

BUFSIZE = 4096

def make_streams_binary():
    sys.stdin = sys.stdin.detach()
    sys.stdout = sys.stdout.detach()

def main():
    parser = OptionParser()
    parser.add_option("-x", help="output hexadecimal code", action = "store_true")
    opt, arg = parser.parse_args()
    tohex = opt.x is not None
    make_streams_binary()
    while True:
        data = sys.stdin.read(BUFSIZE)
        if not data:
            break
        if tohex:
            data = hexlify(data)
        sys.stdout.write(data)

if __name__ == "__main__":
    main()

Then

$ streams.py -x < tmp.jpg

Each pair of consecutive hex digits represents 1 character in the source file. You could also implement an option -u to unhexlify the input :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.