从Python中按顺序运行命令
我试图使用Python构建一个LaTeX文档,但在让命令按顺序运行时遇到了问题。对于那些熟悉LaTeX的人,您将知道您通常必须运行四个命令,每个命令在运行下一个命令之前完成,例如从Python中按顺序运行命令,python,Python,我试图使用Python构建一个LaTeX文档,但在让命令按顺序运行时遇到了问题。对于那些熟悉LaTeX的人,您将知道您通常必须运行四个命令,每个命令在运行下一个命令之前完成,例如 pdflatex file bibtex file pdflatex file pdflatex file 因此,在Python中,我这样做是为了定义命令 commands = ['pdflatex','bibtex','pdflatex','pdflatex'] commands = [(element + ' '
pdflatex file
bibtex file
pdflatex file
pdflatex file
因此,在Python中,我这样做是为了定义命令
commands = ['pdflatex','bibtex','pdflatex','pdflatex']
commands = [(element + ' ' + src_file) for element in commands]
但问题是运行它们
我尝试过使用一些东西,比如在循环中使用os.system()
,子进程
之类的map(call,commands)
或Popen
,并将列表折叠成一个由和分隔的字符串,但看起来这些命令都是作为单独的进程运行的,无需等待上一个完成
作为记录,我使用的是Windows,但我想要一个跨平台的解决方案
编辑
问题是指定src_文件变量时出现错误;它不应该有一个“.tex”。下面的代码现在可以工作了:
test.py
import subprocess
commands = ['pdflatex','bibtex','pdflatex','pdflatex']
for command in commands:
subprocess.call((command, 'test'))
test.tex
\documentclass{article}
\usepackage{natbib}
\begin{document}
This is a test \citep{Body2000}.
\bibliographystyle{plainnat}
\bibliography{refs}
\end{document}
参考比伯
@book{Body2000,
author={N.E. Body},
title={Introductory Widgets},
publisher={Widgets International},
year={2000}
}
os.system
不应导致此问题,但subprocess.Popen
应引起此问题
但我认为使用是最好的选择:
commands = ['pdflatex','bibtex','pdflatex','pdflatex']
for command in commands:
subprocess.call((command, src_file))
os.system
不应导致此问题,但subprocess.Popen
应引起此问题
但我认为使用是最好的选择:
commands = ['pdflatex','bibtex','pdflatex','pdflatex']
for command in commands:
subprocess.call((command, src_file))
如果您使用的是,那么应该可以。我认为这看起来很有希望,但老实说,我并没有真正遵循代码示例。对于一个简单的任务来说,2个类似乎有很多代码。2个类更简单。但是调用相当于Popen(…),然后对返回的对象调用wait()。如果您使用使用的,那应该可以。我认为这看起来很有希望,但老实说,我并不真正遵循代码示例。对于一个简单的任务来说,2个类似乎有很多代码。2个类更简单。但是调用相当于Popen(…),然后对返回的对象调用wait()。不,这并不是按顺序运行它们。LaTeX文档仍然缺少交叉引用。(我已经用手动构建检查了源文件。)没错!在src_文件字符串上使用tex
扩展名会破坏bibtex编译。我已经添加了上面的更改。不-这不会按顺序运行它们。LaTeX文档仍然缺少交叉引用。(我已经用手动构建检查了源文件。)没错!在src_文件字符串上使用tex
扩展名会破坏bibtex编译。我已经在上面添加了更改。