操作系统:Linux。在
我想做的是:用TikZ命令编译一个文件,然后用某个pdf查看器打开它(我只需要使用查看器的名称!)。如果查看器不存在,我必须返回一个异常。在
我的方法是:搜索具有查看器名称的文件。这应该是查看器本身。接下来,我想确定这个文件的路径。tikz_命令。然后我想通过subprocess.call([viewer_path] + ['tikz_commands.pdf']
使用它的值。以下是类的一部分:
import subprocess
import os
import tempfile
import fnmatch
def visualize(self,viewername):
temp = tempfile.mkdtemp()
os.chdir(temp)
file = open('tikz_commands.tex', 'w')
file.write(tikz_commands)
file.close()
proc=subprocess.Popen(['pdflatex','tikz_commands.tex'])
proc.communicate()
subprocess.call([str(self.set_viewer(viewername)), 'tikz_commands.pdf'])
def set_viewer(self,viewername):
try:
for root, dirs, files in os.walk(os.path.join('path', 'to', 'file')):
for file in files:
if str(viewername) in file.lower():
return(os.path.join(root, file))
except NameError:
print('No such viewer')
这行吗?在
我是否忽略了一个更简单的方法?在
非常感谢任何帮助!在
编辑:多亏了@Roland Smith和@Robb,我可以把所有事情都解决了,现在一切都很完美。如果要查看最终代码,请添加注释。在
在类UNIX的操作系统(如Linux)上,通常认为这样的程序安装在环境变量
PATH
中的位置。在这种情况下,您应该能够调用程序,而不需要任何位置,它应该可以正常工作。在您可以使用linux
which
命令查找程序:如果找不到程序
viewer_name
,它将引发一个CalledProcessError
,如果找到,则返回完整路径相关问题 更多 >
编程相关推荐