OS : Linux.
What I want to do: Compile a file with TikZ-commands and open it with a certain pdf-viewer (I have to work only with the name of the viewer!). If the viewer does not exist, I have to return an exception.
My approach: I search for a file with the name of the viewer. This then should be the viewer itself. Next, I want to determine the path to this file. tikz_commands. Then I want to use its value via subprocess.call([viewer_path] + ['tikz_commands.pdf']
. The following is part of a class:
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')
Can this work?
Have I overlooked a much easier approach?
Any help is much appreciated!
EDIT: Thanks to @Roland Smith and @Robb I could work everything out and it works perfectly now. Comment if you want to see the final code.
On UNIX-like operating systems (like Linux) it is generally expected that such a program is installed in a location that is contained in the environment variable PATH
. In that case you should just be able to call the program without any location and it should just work.