Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #86165

Re: calling subprocess

References <edfc56d5-8696-49d0-8b71-69044bbab1f5@googlegroups.com>
Date 2015-02-23 09:21 +1100
Subject Re: calling subprocess
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.19035.1424643724.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, Feb 23, 2015 at 9:06 AM,  <jkuplinsky@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

calling subprocess jkuplinsky@gmail.com - 2015-02-22 14:06 -0800
  Re: calling subprocess Tim Golden <mail@timgolden.me.uk> - 2015-02-22 22:14 +0000
  Re: calling subprocess Chris Angelico <rosuav@gmail.com> - 2015-02-23 09:21 +1100
    Re: calling subprocess jkuplinsky@gmail.com - 2015-02-22 17:13 -0800
      Re: calling subprocess Chris Angelico <rosuav@gmail.com> - 2015-02-23 12:31 +1100
      Re: calling subprocess Dave Angel <davea@davea.name> - 2015-02-22 21:38 -0500
      Re: calling subprocess Dave Angel <davea@davea.name> - 2015-02-22 22:07 -0500

csiph-web