Python utility fails to successfully run a non-Python script that uses relative paths -
my python3 utility has function doesn't work (unless it's placed within selected directories, can run non-python pdflatex
scripts successfully). want run utility set location on of template.tex files have, stored in various other locations.
the python utility prompts user select pdflatex
template file absolute path using tkinter.filedialog gui, runs user's selected pdflatex
script using, example: os.system("pdflatex /afullpath/a/b/c/mytemplate.tex")
python's os.system
runs pdflatex
, runs mytemplate.tex
script. mytemplate.tex
has numerous inputs written relative paths ./d/another.tex
.
so, python utility works fine long it's in exact same path /afullpath/a/b/c/mytemplate.tex
user selects. otherwise pdflatex
can't finds own input files. pdflatex
delivers error message like: ! latex error: file ./d/another.tex not found
because execution path relative python script , not pdflatex
script.
[pdflatex
needs use relative paths because folders .tex files moved around, needed.]
i found following similar case on stack overflow, don't think answers geared towards situation: relative paths in python -- stack overflow
by referring other files relative paths ./d/another.tex
, mytemplate.tex
file assuming (and requiring) pdflatex
run on same directory mytemplate.tex
located in. need satisfy requirement changing directory containing mytemplate.tex
before calling os.system
:
input_file = '/afullpath/a/b/c/mytemplate.tex' olddir = os.getcwd() os.chdir(os.path.dirname(input_file)) os.system('pdflatex ' + input_file) os.chdir(olddir)
even better use subprocess.call
, handles change of directory , isn't vulnerable shell quoting issues:
subprocess.call(['pdflatex', input_file], cwd=os.path.dirname(input_file))
Comments
Post a Comment