calling subprocess

Chris Angelico rosuav at gmail.com
Sun Feb 22 17:21:54 EST 2015


On Mon, Feb 23, 2015 at 9:06 AM,  <jkuplinsky at gmail.com> wrote:
> I thought this would be easy:
>
>
> for subprocess import call
> call (['cd', r'C:\apps'], shell = True)
>
>
> It doesn't work -- tried with/without prefix r, escaped backslashes, triple quotes, str(), .. nothing seems to work (it doesn't complain, but it doesn't change directories either) -- what am I doing wrong?

It does work. But what it does is spawn a shell, change the working
directory _of that shell_, and then terminate it. The working
directory change won't apply to your process.

It sounds to me like you're adopting a "shotgun debugging" [1]
approach. If you don't know what your changes are going to do, why are
you making them? I recommend, instead, a policy of examination and
introspection - what I tend to refer to as IIDPIO debugging: If In
Doubt, Print It Out. The first argument to subprocess.call() is a list
of parameters; you can assign that to a name and print it out before
you do the call:

from subprocess import call
args = ['cd', r'C:\apps']
print(args)
call(args, shell=True)

Now do all your permutations of args, and see what effect they have.
If the printed-out form is identical, there's no way it can affect the
subprocess execution.

Side point: *Copy and paste* your code rather than retyping it. The
keyword here is "from", not "for", and if we can't depend on the code
you're showing us, how can we recognize whether or not a bug exists in
your real code?

ChrisA

[1] http://www.catb.org/jargon/html/S/shotgun-debugging.html



More information about the Python-list mailing list