Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run a binary executable with input file (bash command) in Python?

I have a binary executable named as "abc" and I have a input file called as "input.txt". I can run these with following bash command:

./abc < input.txt

How can I run this bash command in Python, I tried some ways but I got errors.

Edit: I also need the store the output of the command.

Edit2:

I solved with this way, thanks for the helps.

input_path = path of the input.txt file.

out = subprocess.Popen(["./abc"],stdin=open(input_path),stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout,stderr = out.communicate()
print(stdout)
like image 974
mustafayd Avatar asked Jul 27 '26 16:07

mustafayd


1 Answers

use os.system

import os
os.system("echo test from shell");
like image 170
FangQ Avatar answered Jul 29 '26 04:07

FangQ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!