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 pdflatexscript 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

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -