从python 3.3运行终端命令

时间:2015-11-12 08:15:06

标签: python shell python-3.x terminal command

我正在尝试从python 3.3(mac osx 10.7.5)运行wkhtmltopdf(一个将html转换为pdf:http://wkhtmltopdf.org的例程)命令。

使用以下命令直接在终端上运行时,例程就像魅力一样:

wkhtmltopdf http:google.com /Users/ME/Desktop/google.pdf

其中http:google.com是将在目标文件中转换为pdf的html:/Users/ME/Desktop/google.pdf

但是从python调用shell时:

import subprocess

subprocess.call(["wkhtmltopdf http:google.com /Users/ME/Desktop/google.pdf"])

我得到:

> Traceback (most recent call last):   File "<pyshell#5>", line 1, in
<module>
    subprocess.call(["wkhtmltopdf http:google.com /Users/thenoze/Desktop/google.pdf"])   File
"/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/subprocess.py",
line 523, in call
    with Popen(*popenargs, **kwargs) as p:   File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/subprocess.py",
line 824, in __init__
    restore_signals, start_new_session)   File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/subprocess.py",
line 1448, in _execute_child
    raise child_exception_type(errno_num, err_msg) FileNotFoundError: [Errno 2] No such file or directory: 'wkhtmltopdf http:google.com /Users/ME/Desktop/google.pdf'

- &GT;任何领导如何格式化我的subprocess.call()?

1 个答案:

答案 0 :(得分:0)

将命令行参数分隔为列表项:

subprocess.call(["wkhtmltopdf", "http:google.com", "/Users/ME/Desktop/google.pdf"])

否则,wkhtmltopdf ... google.pdf被视为程序路径,而不是wkhtmltopdf