سؤال

I need to call an executable in a python script and also pass binary data (generated in the same script) to this executable.

I have it working like so:

bin = make_config(data)
open('binaryInfo.bin', 'wb+').write(bin)

os.system("something.exe " + "binaryInfo.bin")

I thought I could avoid creating the binaryInfo.bin file altogether by passing 'bin' straight to the os.system call:

bin = make_config(data)

os.system("something.exe " + bin)

But in this case I get an error: "Can't convert 'bytes' object to str implicitly"

Does anyone know the correct syntax here? Is this even possible?

هل كانت مفيدة؟

المحلول

Does anyone know the correct syntax here? Is this even possible?

Not like you're doing it. You can't pass arbitrary binary data on the UNIX command line, as each argument is inherently treated as null-terminated, and there's a maximum total length limit which is typically 64KB or less.

With some applications which recognize this convention, you may be able to pipe data on stdin using something like:

pipe = os.popen("something.exe -", "w")
pipe.write(bin)
pipe.close()

If the application doesn't recognize "-" for stdin, though, you will probably have to use a temporary file like you're already doing.

نصائح أخرى

os.system(b"something.exe " + bin)

Should do it.. However, I'm not sure you should be sending binary data through the command line. There might be some sort of limit on character count. Also, does this something.exe actually accept binary data through the command line even?

how bout base64encoding it before sending and decoding on the other end... afaik command line arguments must be ascii range values (although this maynot be true... but I think it is..) ...

another option would be to do it the way you currently are and passing the file ...

or maybe see this Passing binary data as arguments in bash

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top