Re: [Tutor] How to pass a python variable to subprocess.call?

2010-10-25 Thread Abhijeet Rastogi
On Tue, Oct 26, 2010 at 3:31 AM, Sean Carolan  wrote:

> I'm rewriting a bunch of my bash scripts to get some python practice.
> There are some instances where python doesn't have the built-in text
> processing that I need, so I'm using subprocess.call.  How can I pass
> a python variable to these subprocesses?  For example:
>
> mydir = "/usr/local/bin"
> subprocess.call("ls -l /usr/local/bin", shell=True)
>
> subprocess.call("ls -l "+mydir,shell=True)
will do the job. In python, we can simply concatenate the strings like that.

How do I replace /usr/local/bin in the subprocess call with the mydir
> variable?
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Abhijeet Rastogi (shadyabhi)
http://www.google.com/profiles/abhijeet.1989
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess Popen PIPE error

2010-10-26 Thread Abhijeet Rastogi
Please read the documentation of Popen. You cannot pass arguments like that.

>>> from subprocess import Popen,PIPE
>>> import shlex
>>> cmd="ls -l"
>>> cmd=shlex.split(cmd)
>>> p = Popen(cmd,stdout=PIPE,stderr=PIPE)

or simply that means

>>> p = Popen(['ls','-l'],stdout=PIPE,stderr=PIPE)

Hope I have made my point clear. The problem occuring is that there is no
binary that has name as "ls -l". Python cannot understand that "-l" is an
argument until you pass it this way.


On Tue, Oct 26, 2010 at 6:52 PM, Sean Carolan  wrote:

> What am I doing wrong here?
>
> >>> from subprocess import Popen, PIPE
> >>> cmd = 'ls -l'
> >>> p = Popen(cmd, stdout=PIPE, stderr=PIPE)
> Traceback (most recent call last):
>  File "", line 1, in ?
>  File "/usr/lib64/python2.4/subprocess.py", line 550, in __init__
>errread, errwrite)
>  File "/usr/lib64/python2.4/subprocess.py", line 993, in _execute_child
>raise child_exception
> OSError: [Errno 2] No such file or directory
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Abhijeet Rastogi (shadyabhi)
http://www.google.com/profiles/abhijeet.1989
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess Popen PIPE error

2010-10-26 Thread Abhijeet Rastogi
Ya. Do it using python. Why do you want to use bash when you already have
python?

See, do something like this:-

import os
alldirs = os.listdir("/path/to/dir")

DIRS = [] #Only the dirs you are interested in

for i in alldirs:
  if i.find("deploy") is -1: L.append(i)
  if i.find("TEMPLATE") is -1: L.append(i)

#Now L contains all the required dirs.

Hope it helps.

On Tue, Oct 26, 2010 at 9:19 PM, Sean Carolan  wrote:

> > Here is the bash one-liner that generates my list,
> > with one name per line:
> >
> > ls -d */ | grep -v -E 'deploy|TEMPLATE' | sed 's/\///'
> >
> > How would you get the output of this into a python list that could
> > then be used in the script?  Please forgive my ignorance; I've read
> > through the documentation but am still not clear on this aspect.
>
> Would this be easier to accomplish using os.listdir()?.  Basically I
> want to make a list of directories, excluding files and the
> directories or files containing "deploy" and "TEMPLATE".
>



-- 
Abhijeet Rastogi (shadyabhi)
http://www.google.com/profiles/abhijeet.1989
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] syntax error that i cant spot!

2011-01-01 Thread Abhijeet Rastogi
You missed a "+" after myName on line 30.

On Sat, Jan 1, 2011 at 10:32 PM, pete  wrote:

> Hi,
> Please help just starting out and have come up with the following code to
> create a simple guessing game.
>
> on line 30 print good job etc i get a syntax error! sure it's simple but
> i've looked for ages and cant spot it!
>
> Regards
> Pete
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Abhijeet Rastogi (shadyabhi)
http://www.google.com/profiles/abhijeet.1989
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor