4

Python 2.4.4 の .tex ファイルで pdflatex を実行しようとしています。サブプロセス (Mac の場合):

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

これは事実上何もしません。ただし、ターミナルで「pdflatex fullpathtotexfile」を問題なく実行でき、pdf を生成できます。私は何が欠けていますか?

[編集]回答の1つで提案されているように、私は試しました:

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)
4

2 に答える 2

5

便利な関数subprocess.callを使用します

Popenここで使用する必要はありませんcall。十分なはずです。

例えば:

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

呼び出しが成功した場合は、return_value0に設定されます。それ以外の場合は1に設定されます。

の使用法Popenは、通常、出力を保存する場合に使用します。たとえば、コマンドunameを使用してカーネルリリースを確認し、それを変数に格納するとします。

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

繰り返しますが、決して設定しないでshell=Trueください。

于 2010-11-20T05:14:15.060 に答える
1

次のいずれかが必要になる場合があります。

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

また

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

(このサブプロセスのドキュメントページセクションから恥知らずにリッピングされました)。

于 2010-11-20T04:01:42.557 に答える