Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.python

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 07-30-2012, 07:40 AM
Chris Angelico
Guest
 
Posts: n/a
Default Re: Linux shell to python

On Mon, Jul 30, 2012 at 5:05 PM, Vikas Kumar Choudhary
<vikas.choudhary@yahoo.co.in> wrote:
>
> I was trying porting from bash shell to python.
>
> let me know if someone has tried to implement (grep and PIPE) shell commands in python `lspci | grep Q | grep "$isp_str1" | grep "$isp_str2" | cut -c1-7'


Welcome!

While it's technically possible to do exactly that in Python (using
subprocess as you describe), there's usually a more efficient and
cleaner method of achieving the same goal. With a port such as you
describe, it's probably best to go right back to the conceptual level
and work out what exactly you're trying to do, and then look at
implementing that in Python. You'll end up with much cleaner code at
the end of it.

For an initial guess, I would say that you'll use subprocess to invoke
lspci, but then everything else will be done in Python directly.

ChrisA
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 07-30-2012, 11:09 AM
张少华
Guest
 
Posts: n/a
Default Re: Linux shell to python

you can use commands.getstatusoutput(command), the shell command special charactor (like "$ and so on )should be escaped!


在 2012年7月30日星期一UTC+8下午3时40分04秒 ,Chris Angelico写道:
> On Mon, Jul 30, 2012 at 5:05 PM, Vikas Kumar Choudhary
>
> <vikas.choudhary@yahoo.co.in> wrote:
>
> >

>
> > I was trying porting from bash shell to python.

>
> >

>
> > let me know if someone has tried to implement (grep and PIPE) shell commands in python `lspci | grep Q | grep "$isp_str1" | grep "$isp_str2" | cut -c1-7'

>
>
>
> Welcome!
>
>
>
> While it's technically possible to do exactly that in Python (using
>
> subprocess as you describe), there's usually a more efficient and
>
> cleaner method of achieving the same goal. With a port such as you
>
> describe, it's probably best to go right back to the conceptual level
>
> and work out what exactly you're trying to do, and then look at
>
> implementing that in Python. You'll end up with much cleaner code at
>
> the end of it.
>
>
>
> For an initial guess, I would say that you'll use subprocess to invoke
>
> lspci, but then everything else will be done in Python directly.
>
>
>
> ChrisA

Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 05:03 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.