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


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

Re: Linux shell to python

Started byChris Angelico <rosuav@gmail.com>
First post2012-07-30 17:40 +1000
Last post2012-07-30 04:09 -0700
Articles 2 — 2 participants

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Linux shell to python Chris Angelico <rosuav@gmail.com> - 2012-07-30 17:40 +1000
    Re: Linux shell to python 张少华 <zhangshaohua20101010@gmail.com> - 2012-07-30 04:09 -0700

#26233 — Re: Linux shell to python

FromChris Angelico <rosuav@gmail.com>
Date2012-07-30 17:40 +1000
SubjectRe: Linux shell to python
Message-ID<mailman.2716.1343634007.4697.python-list@python.org>
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

[toc] | [next] | [standalone]


#26244

From张少华 <zhangshaohua20101010@gmail.com>
Date2012-07-30 04:09 -0700
Message-ID<8f988469-5d04-4d89-9856-b37b5fbe79e1@googlegroups.com>
In reply to#26233
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

[toc] | [prev] | [standalone]


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


csiph-web