title: Find path to pdf-reader and open file with it
body: <p>OS : Linux.</p>
  
  <p>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.</p>
  
  <p>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 <code>subprocess.call([viewer_path] + ['tikz_commands.pdf']</code>. The following is part of a class:</p>
  
  <pre><code>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')
  </code></pre>
  
  <p>Can this work?</p>
  
  <p>Have I overlooked a much easier approach?</p>
  
  <p>Any help is much appreciated!  </p>
  
  <p>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.</p>