代码之家  ›  专栏  ›  技术社区  ›  Benjamin Cintix

mac上python子进程中的pdflatex

  •  4
  • Benjamin Cintix  · 技术社区  · 14 年前

    我试图在Python 2.4.4中的.tex文件上运行pdflatex。子进程(在mac上):

    import subprocess
    subprocess.Popen(["pdflatex", "fullpathtotexfile"], shell=True)
    

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

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

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

    失败的原因是:

    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
    

    文件确实存在,我可以运行。 pdflatex /Users/Benjamin/Desktop/directory/ON.tex 在终点站。请注意,pdflatex确实抛出了大量警告。。。但这不重要,这也会产生同样的错误:

    return_value = subprocess.call(['pdflatex', '-interaction=batchmode', '/Users/Benjamin/Desktop/directory/ON.tex'], shell =False)
    
    2 回复  |  直到 14 年前
        1
  •  4
  •   user225312    14 年前

    使用便利功能, subprocess.call

    你不需要使用 Popen 在这里, call 应该够了。

    例如:

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

    如果通话成功, return_value 将设置为0,否则为1。

    使用 波本 通常用于需要存储输出的情况。例如,您希望使用以下命令检查内核版本 uname 并将其存储在某个变量中:

    >>> process = subprocess.Popen(['uname', '-r'], shell=False, stdout=subprocess.PIPE)
    >>> output = process.communicate()[0]
    >>> output
    '2.6.35-22-generic\n'
    

    再一次,永远不要 shell=True .

        2
  •  1
  •   mjhm    14 年前

    您可能需要:

    output = Popen(["pdflatex", "fullpathtotexfile"], stdout=PIPE).communicate()[0]
    print output
    

    p = subprocess.Popen(["pdflatex" + " fullpathtotexfile"], shell=True)
    sts = os.waitpid(p.pid, 0)[1]
    

    (不知羞耻地从这个 subprocess doc page section ).