0

I would like to know how to use "subprocess module" to rewrite the following code:

import commands
print commands.getoutput('convert -quality 100 ___t*.png images/transient_heat.gif')
print commands.getoutput('rm ___t*.png')

Because "commands module" does not support Python 3, I would like to use "subprocess module". I tried the following code but it does not work.

import subprocess
print ( subprocess.Popen('convert -quality 100 ___t*.png images/transient_heat.gif') )
print ( subprocess.Popen('rm ___t*.png') )

Thank you very much! The code was originally taken from Kitchin's blog: http://kitchingroup.cheme.cmu.edu/blog/2013/03/07/Transient-heat-conduction-partial-differential-equations/

2
  • How do you know it doesn't work? Edit the question. Commented Jun 8, 2017 at 22:33
  • @PeterWood I ran the code but I cannot generate the output file "images/transient_heat.gif". Thank you! Commented Jun 8, 2017 at 23:11

2 Answers 2

2

subprocess expects a list of strings that form the command. You could split your commands up by hand like so:

import subprocess
print(subprocess.Popen(['convert', '-quality', '100', '___t*.png', 'images/transient_heat.gif']))
print (subprocess.Popen(['rm', '___t*.png']))

or, you could let shlex.split() do the work for you:

import shlex
import subprocess
print (subprocess.Popen(shlex.split('convert -quality 100 ___t*.png images/transient_heat.gif')))
print (subprocess.Popen(shlex.split('rm ___t*.png')))

EDIT:

The following code will show you the output of stdout and stderr when it executes, which may help you fix any mistakes in your command line.

import shlex
import subprocess

convert_proc = subprocess.Popen(shlex.split('convert -quality 100 ___t*.png images/transient_heat.gif'), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
outs, errs = convert_proc.communicate()
print('STDOUT: {}'.format(outs))
print('STDERR: {}'.format(errs))

subprocess.Popen(shlex.split('convert -quality 100 ___t*.png images/transient_heat.gif'))
print (subprocess.Popen(shlex.split('rm ___t*.png')))
Sign up to request clarification or add additional context in comments.

2 Comments

I tried both codes but they cannot output "'images/transient_heat.gif'". Thank you!
Try using the full path to the convert utility, instead of just "convert". You can find the full path out by executing "which convert" from a shell prompt.
0

You can just use subprocess.check_output

print subprocess.check_output('rm ___t*.png', shell=True)

4 Comments

When I did not, it returns the error message: CalledProcessError: Command 'convert -quality 100 ___t*.png images/transient_heat.gif' returned non-zero exit status 4. Could you give me a help? Thank you!
@world2005 did you use shell=True
yes, I used shell=True. The codes are subprocess.check_output(['convert', '-quality', '100', '___t*.png', 'images/transient_heat.gif'], shell=True) and subprocess.check_output(['rm ___t*.png'], shell=True). Thank you!
If you're passing a list of commands, you shouldn't use shell. Also, each item in the list should only contain one argument -- ['rm', '___t*.png']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.