运行 python 中的程序提示
Running program prompt in python
我想知道如何在 python 中使用命令提示符。事情是这样的,我需要 运行 一个基于 python 的程序,我以前是在命令提示符下做的。但是,我需要多次 运行 这个程序,因此,我想自动化它。该程序需要 运行 特定文件夹中的文件,并且它使用位于同一特定文件夹中的配置文件。最后,我还需要它在完成每个过程后提供一个日志文件。我曾经在命令提示符下执行所有这些操作:
C:\Users\Gabriel\Documents\vina_tutorial>"\Program Files (x86)\The Scripps Research Institute\Vina\vina.exe" --config conf.txt --log log.txt
我尝试使用 python:
import subprocess
subprocess.Popen('C:\Program Files (x86)\The Scripps Research Institute\Vina\vina.exe -config ' + 'conf.txt', cwd='C:\Users\Gabriel\Documents\vina_tutorial')
然而,它似乎没有用。 (我在第一步中确实省略了日志文件)
关于如何继续或我可以在哪里学习的任何提示?
您需要将 shell 命令拆分成单独的参数传递给 Popen。 Read the documentation
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
此外,您可能需要转义 Windows 文件路径中的反斜杠。您可能还需要附上引号 IE '"C:\Program Files (x86)\etc..\foo.exe"'
我想知道如何在 python 中使用命令提示符。事情是这样的,我需要 运行 一个基于 python 的程序,我以前是在命令提示符下做的。但是,我需要多次 运行 这个程序,因此,我想自动化它。该程序需要 运行 特定文件夹中的文件,并且它使用位于同一特定文件夹中的配置文件。最后,我还需要它在完成每个过程后提供一个日志文件。我曾经在命令提示符下执行所有这些操作:
C:\Users\Gabriel\Documents\vina_tutorial>"\Program Files (x86)\The Scripps Research Institute\Vina\vina.exe" --config conf.txt --log log.txt
我尝试使用 python:
import subprocess
subprocess.Popen('C:\Program Files (x86)\The Scripps Research Institute\Vina\vina.exe -config ' + 'conf.txt', cwd='C:\Users\Gabriel\Documents\vina_tutorial')
然而,它似乎没有用。 (我在第一步中确实省略了日志文件)
关于如何继续或我可以在哪里学习的任何提示?
您需要将 shell 命令拆分成单独的参数传递给 Popen。 Read the documentation
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
此外,您可能需要转义 Windows 文件路径中的反斜杠。您可能还需要附上引号 IE '"C:\Program Files (x86)\etc..\foo.exe"'