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


Groups > comp.lang.python > #86161 > unrolled thread

calling subprocess

Started byjkuplinsky@gmail.com
First post2015-02-22 14:06 -0800
Last post2015-02-22 22:07 -0500
Articles 7 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#86161 — calling subprocess

Fromjkuplinsky@gmail.com
Date2015-02-22 14:06 -0800
Subjectcalling subprocess
Message-ID<edfc56d5-8696-49d0-8b71-69044bbab1f5@googlegroups.com>
Hi,

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?

Thanks.

JK

[toc] | [next] | [standalone]


#86164

FromTim Golden <mail@timgolden.me.uk>
Date2015-02-22 22:14 +0000
Message-ID<mailman.19034.1424643312.18130.python-list@python.org>
In reply to#86161
On 22/02/2015 22:06, jkuplinsky@gmail.com wrote:
> Hi,
>
> 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?

Two things:

1) All you're doing is running up a subprocess, changing directory 
*within it* and then closing it. (Altho' good job spotting that you'd 
need shell=True if you did indeed want to do what you're doing here).

2) Use os.chdir: that's what it's there for

TJG

[toc] | [prev] | [next] | [standalone]


#86165

FromChris Angelico <rosuav@gmail.com>
Date2015-02-23 09:21 +1100
Message-ID<mailman.19035.1424643724.18130.python-list@python.org>
In reply to#86161
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

[toc] | [prev] | [next] | [standalone]


#86172

Fromjkuplinsky@gmail.com
Date2015-02-22 17:13 -0800
Message-ID<4e828264-5445-42fc-a647-c56f80fc86ab@googlegroups.com>
In reply to#86165
On Sunday, February 22, 2015 at 5:22:24 PM UTC-5, Chris Angelico wrote:
> 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

OK (1) sorry about for/from
(2) print() sounds nice, but fact is , no matter what I try, i always get C:\\apps instead of c:\apps. So in this sense print() doesn't help much. Obviously i'm doing something wrong -- which is what you perhaps call shotgun debugging; but that's why i'm asking. 

[toc] | [prev] | [next] | [standalone]


#86174

FromChris Angelico <rosuav@gmail.com>
Date2015-02-23 12:31 +1100
Message-ID<mailman.19039.1424655074.18130.python-list@python.org>
In reply to#86172
On Mon, Feb 23, 2015 at 12:13 PM,  <jkuplinsky@gmail.com> wrote:
> (2) print() sounds nice, but fact is , no matter what I try, i always get C:\\apps instead of c:\apps. So in this sense print() doesn't help much. Obviously i'm doing something wrong -- which is what you perhaps call shotgun debugging; but that's why i'm asking.
>

Actually, that means it's helping a lot: it's showing you that, no
matter what you fiddle with in terms of string literals, the resulting
string is exactly the same. That's the point of printing stuff out :)

ChrisA

[toc] | [prev] | [next] | [standalone]


#86179

FromDave Angel <davea@davea.name>
Date2015-02-22 21:38 -0500
Message-ID<mailman.19042.1424659124.18130.python-list@python.org>
In reply to#86172
On 02/22/2015 08:13 PM, jkuplinsky@gmail.com wrote:
>
> OK (1) sorry about for/from

That's not what you should be sorry about.  You should be sorry you 
didn't use cut&paste.

> (2) print() sounds nice, but fact is , no matter what I try, i always get C:\\apps instead of c:\apps. So in this sense print() doesn't help much. Obviously i'm doing something wrong -- which is what you perhaps call shotgun debugging; but that's why i'm asking.
>

You probably are getting confused about the difference between str() and 
repr().  If you print the repr() of a string, it'll add quotes around 
it, and escape the unprintable codes.  So it'll double the backslash. 
It also turns a newline into  \n, and tabs into \t, and so on.  Very useful.

That's also what happens when you print a list that contains strings. 
The individual elements of the list are converted using repr().  Watch 
for the quotes to get a strong hint about what you're seeing.

If you don't get a positive handle on how string literals relate to 
string variables, and on str() and repr(), and print(), you'll be 
jumping around the problem instead of solving it.



Back to your original problem, which had you trying to use 
subprocess.call to change the current directory.  Current directory is 
effectively (or actually) depending on the OS involved) an environment 
variable, and changes made in a child process are not magically returned 
to the parent.

But even though there is an os.chdir() in Python, you really shouldn't 
use it.  Long experience of many people show that you're better off 
manipulating the directories you need explicitly, converting any 
directory that's relative to something other than the current one, to an 
absolute.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#86185

FromDave Angel <davea@davea.name>
Date2015-02-22 22:07 -0500
Message-ID<mailman.19047.1424660848.18130.python-list@python.org>
In reply to#86172
On 02/22/2015 09:38 PM, Dave Angel wrote:
> On 02/22/2015 08:13 PM, jkuplinsky@gmail.com wrote:
>>
>> OK (1) sorry about for/from
>
> That's not what you should be sorry about.  You should be sorry you
> didn't use cut&paste.
>
>> (2) print() sounds nice, but fact is , no matter what I try, i always
>> get C:\\apps instead of c:\apps. So in this sense print() doesn't help
>> much. Obviously i'm doing something wrong -- which is what you perhaps
>> call shotgun debugging; but that's why i'm asking.
>>
>
> You probably are getting confused about the difference between str() and
> repr().  If you print the repr() of a string, it'll add quotes around
> it, and escape the unprintable codes.  So it'll double the backslash. It
> also turns a newline into  \n, and tabs into \t, and so on.  Very useful.
>
> That's also what happens when you print a list that contains strings.
> The individual elements of the list are converted using repr().  Watch
> for the quotes to get a strong hint about what you're seeing.
>
> If you don't get a positive handle on how string literals relate to
> string variables, and on str() and repr(), and print(), you'll be
> jumping around the problem instead of solving it.
>
Two other things I should have pointed out here.  The debugger uses 
repr() to display things, when you have an unassigned expression.

And you can solve a lot of problems by just using a forward slash for a 
directory separator.  The forward slash is just as correct in most 
circumstances within a program.  It's mainly on the command line that 
forward slash takes on a different meaning.

>
>
> Back to your original problem, which had you trying to use
> subprocess.call to change the current directory.  Current directory is
> effectively (or actually) depending on the OS involved) an environment
> variable, and changes made in a child process are not magically returned
> to the parent.
>
> But even though there is an os.chdir() in Python, you really shouldn't
> use it.  Long experience of many people show that you're better off
> manipulating the directories you need explicitly, converting any
> directory that's relative to something other than the current one, to an
> absolute.
>


-- 
DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web