subprocess.Popen syntax

This is the place for queries that don't fit in any of the other categories.

subprocess.Popen syntax

Postby corvi » Wed Oct 16, 2013 1:04 pm

I am new for python.
I must call the executable "pdftk" inside a python script loop to merge
pdf files into a single pdf.
The shell command is: pdftk file1.pdf file2.pdf cat output file3.pdf
(the existing files file1.pdf and file2.pdf are merged into the new file file3.pdf)
pdf files are correctly created by gnuplot calls inside the python script via the command:
subprocess.Popen("gnuplot /path to gnuplot script.../dofit.gnu", shell = True)

I tried with the commands:
Code: Select all
os.environ['old'] = "/path to file../file1.pdf"
os.environ['new'] = "/path to file../file2.pdf"
os.environ['sum'] = "/path to file../file3.pdf"         
subprocess.Popen("pdftk $old $new cat output $sum", shell = True)


The error messages are:
Code: Select all
Error: Unable to find file.
Error: Failed to open PDF file:


the same command executed in bash shell works well.
The syntax of "subprocess.Popen(..." is probably wrong...
Last edited by stranac on Wed Oct 16, 2013 1:13 pm, edited 1 time in total.
Reason: Added code tags.
corvi
 
Posts: 4
Joined: Sun Sep 08, 2013 7:40 am

Re: subprocess.Popen syntax

Postby stranac » Wed Oct 16, 2013 1:18 pm

No, the syntax is ok, and it could be made to work this way, but there is a better way.
You should be passing a list of arguments to subprocess.Popen() instead of a string.
Also, there is probably no need to use shell=True for this.

Something like this should work:
Code: Select all
old = "/path to file../file1.pdf"
new = "/path to file../file2.pdf"
the_sum = "/path to file../file3.pdf" # since sum() is the name of a built-in
subprocess.Popen(['pdftk', old, new, 'cat', 'output', the_sum])
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: subprocess.Popen syntax

Postby corvi » Wed Oct 16, 2013 2:17 pm

thanks a lot, my mistake should be elsewhere..
I'l follow your suggestion. Maybe " sum" must be changed in "the_sum" also inside subprocess.Popen(...
corvi
 
Posts: 4
Joined: Sun Sep 08, 2013 7:40 am

Re: subprocess.Popen syntax

Postby stranac » Wed Oct 16, 2013 2:22 pm

Sure, I forgot to change that one.
Will edit my post.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: subprocess.Popen syntax

Postby corvi » Thu Oct 17, 2013 5:26 am

I simplified my script extracting the crucial lines:
Code: Select all
#!/usr/bin/python
import subprocess
import os
file1 = 'old.pdf'
file2 = 'new.pdf'
file3 = 'sum.pdf'
print os.path.isfile(file1)
print os.path.isfile(file2)
print os.path.isfile(file3)
print '>>>>>>>> call to pdftk'
subprocess.Popen(['pdftk', file1, file2, 'cat', 'output', file3])
print os.path.isfile(file1)
print os.path.isfile(file2)
print os.path.isfile(file3)

where the output is:
Code: Select all
True
True
False
>>>>>>>> call to pdftk
True
True
False

after pdftk call, file3 for python is False, but file3 has been instead regularly created and exists!
This point is important to me, because in my script the call to pdftk is inserted in a loop where every time I would rename file3 to file1, merging e new file2 and create a new file3, up to the end of the loop..
But the procedure immediately fails inside the first loop because for python file3 is a non existing file.
Can you explain why this happens and (hopefully) how to overcome this problem?
Thanks!
corvi
 
Posts: 4
Joined: Sun Sep 08, 2013 7:40 am

Re: subprocess.Popen syntax

Postby stranac » Thu Oct 17, 2013 7:43 am

subprocess.Popen() won't wait for the subprocess to finish.
If you want that, use subprocess.call() instead, or sumply use .wait() on the created subprocess.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: gutschy and 4 guests