英文:
Run shell commands with Python subprocess
问题
I encountered a problem.
I want to run a command: leaks -atExit -- ./a.out < test.in | grep LEAK
in subprocess in Python. But instead of input being passed with < test.in
, I want to take it from a variable.
Please help.
In a different part of my code, I tried:
res = subprocess.run(['./a.out'], capture_output=True, text=True, input=self.input_text, check=True)
and it worked perfectly. But when I want to do it similarly,
res = subprocess.run(['leaks', '-atExit', '--', './a.out', '|', 'grep', 'LEAK'], capture_output=True, text=True, input=self.input_text, check=True)
It does not work.
英文:
i encountered a problem.
I want to run a command: leaks -atExit -- ./a.out < test.in | grep LEAK
in subprocess in Python. But instead of input beeing passed with < test.in
, I want to take it from variable.
Please help.
In the different part of my code, I tried:
res = subprocess.run(['./a.out'], capture_output=True, text=True, input=self.input_text, check=True)
and it worked perfecly. But when I want to do it simmilarly,
res = subprocess.run(['leaks', '-atExit', '--', './a.out', '|', 'grep',' LEAK'], capture_output=True, text=True, input=self.input_text, check=True)
It does not work.
答案1
得分: 1
我成功解决了我的问题。
command = 'leaks -atExit -- ./a.out <' + str(self.input_filepath) + '| grep LEAK'
res = subprocess.run(command, text=True, capture_output=True, shell=True)
工作了
英文:
I managed to resolve my problem.
command = 'leaks -atExit -- ./a.out <' + str(self.input_filepath) + '| grep LEAK'
res = subprocess.run(command, text=True, capture_output=True, shell=True)
Worked
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论