当前位置:首页 > 操作系统 > MacOs

pdflatex在mac上的python子进程中

我正在尝试从Python 2.4.4在.tex文件上运行pdflatex.子进程(在mac上):

<code>import subprocess
subprocess.Popen(["pdflatex", "fullpathtotexfile"], shell=True)
</code>

这实际上什么也没做.但是,我可以在终端中运行“pdflatex fullpathtotexfile”而不会出现问题,生成pdf.我错过了什么?

[编辑]
正如其中一个答案所示,我试过:

<code>return_value = subprocess.call(['pdflatex', '/Users/Benjamin/Desktop/directory/ON.tex'], shell =False)
</code>

失败的是:

<code>Traceback (most recent call last):
  File "/Users/Benjamin/Desktop/directory/generate_tex_files_v3.py", line 285, in -toplevel-
    return_value = subprocess.call(['pdflatex', '/Users/Benjamin/Desktop/directory/ON.tex'], shell =False)
  File "/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/subprocess.py", line 413, in call
    return Popen(*args, **kwargs).wait()
  File "/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/subprocess.py", line 543, in __init__
    errread, errwrite)
  File "/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/subprocess.py", line 975, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
</code>

该文件确实存在,我可以在终端中运行pdflatex /Users/Benjamin/Desktop/directory/ON.tex.请注意,pdflatex确实会抛出大量警告……但这无关紧要,这也会产生同样的错误:

<code>return_value = subprocess.call(['pdflatex', '-interaction=batchmode', '/Users/Benjamin/Desktop/directory/ON.tex'], shell =False)
</code>

解决方法:

使用便利功能,subprocess.call

你不需要在这里使用Popen,打电话就足够了.

例如:

<code>>>> import subprocess
>>> return_value = subprocess.call(['pdflatex', 'textfile'], shell=False) # shell should be set to False
</code>

如果调用成功,则return_value将设置为0,否则为1.

Popen的用法通常适用于您希望存储输出的情况.例如,您想使用命令uname检查内核版本并将其存储在某个变量中:

<code>>>> process = subprocess.Popen(['uname', '-r'], shell=False, stdout=subprocess.PIPE)
>>> output = process.communicate()[0]
>>> output
'2.6.35-22-genericn'
</code>

再次,永远不要设置shell = True.


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐