Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: output from external commands

Reply
Thread Tools

Re: output from external commands

 
 
darren kirby
Guest
Posts: n/a
 
      10-24-2005
quoth the James Colannino:
> Hey everyone. First off, I'm new to the list. I had had a little bit
> of experience with Perl before discovering Python. The more Python I
> learn, the more I love it I just have a quick question to ask. I
> know that this is probably a simple question, but I've been googling
> around, and partly because I'm not sure exactly what to search for, I've
> been unsuccessful at finding an answer. What I'd like to do is be able
> to take the output of an external command and assign it as an array of
> strings. So, for example, in Perl I could do something like:
>
> @files = `ls`;
>
> So I guess I'm looking for something similiar to the backticks in Perl.
> Forgive me if I've asked something that's a bit basic for this list.
> Any help would be greatly appreciated Thanks very much in advance.


If all you want is filenames this will work:
>>> import glob
>>> files = ["%s" % f for f in glob.glob("*")]


Else use os.popen to iterate over lines of output:
>>> import os
>>> for line in os.popen("ls -l").readlines():
>>> . . . process(line)


Or check out subprocess if you have 2.4..

> James
>
> --
> My blog: http://www.crazydrclaw.com/
> My homepage: http://james.colannino.org/


-d
--
darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBDXJMMwPD5Cr/3CJgRAif8AJ9D/AS1/VPDRVssFj5JBBRjDu4tYgCg2W0r
wXWfMbNJNk1lYFT48TwFUgc=
=PgVa
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
 
Kent Johnson
Guest
Posts: n/a
 
      10-24-2005
darren kirby wrote:
> quoth the James Colannino:
>>So, for example, in Perl I could do something like:
>>
>>@files = `ls`;
>>
>>So I guess I'm looking for something similiar to the backticks in Perl.
>>Forgive me if I've asked something that's a bit basic for this list.
>>Any help would be greatly appreciated Thanks very much in advance.

>
>
> If all you want is filenames this will work:
>
>>>>import glob
>>>>files = ["%s" % f for f in glob.glob("*")]


or
import os
files = os.listdir('.')

Python has built-in support for many file manipulations, see the os, os.path and shutil modules.

Kent
 
Reply With Quote
 
 
 
 
Mike Meyer
Guest
Posts: n/a
 
      10-24-2005
darren kirby <> writes:
> If all you want is filenames this will work:
>>>> import glob
>>>> files = ["%s" % f for f in glob.glob("*")]


What's the point of doing "%s" % f? How is this different from just
file = [f for f in glob.glob("*")]?

<mike
--
Mike Meyer <> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      10-24-2005
Mike Meyer wrote:
> darren kirby <> writes:
>
>>If all you want is filenames this will work:
>>
>>>>>import glob
>>>>>files = ["%s" % f for f in glob.glob("*")]

>
>
> What's the point of doing "%s" % f? How is this different from just
> file = [f for f in glob.glob("*")]?


Answering narrowly, the difference is that using "%s" calls str() on the
items in the result list, while your suggestion does not. ("Why not
just use str(f) instead of the less clear '%s' % f?" would be a valid
question too though.)

-Peter
 
Reply With Quote
 
James Colannino
Guest
Posts: n/a
 
      10-24-2005
Kent Johnson wrote:

>import os
>files = os.listdir('.')
>


Thanks, that's good to know. I still need to use os.popen() for a few
things, but I'll be needing filenames also, so when I try to get
filenames I'll use the above.

James

--
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/

"If Carpenters made houses the way programmers design programs, the first woodpecker to come along would destroy all of civilization." --Computer Proverb

 
Reply With Quote
 
Terry Hancock
Guest
Posts: n/a
 
      10-24-2005
On Monday 24 October 2005 11:24 am, Peter Hansen wrote:
> Answering narrowly, the difference is that using "%s" calls str() on the
> items in the result list, while your suggestion does not. ("Why not
> just use str(f) instead of the less clear '%s' % f?" would be a valid
> question too though.)


The answer to which, in my experience, is to provide consistency with
code that uses a non-trivial format string, such as '"Error: %s" % f',
and/or to suggest to future developers (including me) that this is the
"right" way to make such changes.

Note also that for those who count, "str(f)" is exactly as long
(in keystrokes) as "'%s'%f", making the "just" a matter of opinion.

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      10-24-2005
Terry Hancock wrote:

> Note also that for those who count, "str(f)" is exactly as long
> (in keystrokes) as "'%s'%f", making the "just" a matter of opinion.


the % implementation still has to create an overallocated output buffer,
parse the format string, call str() on the argument, verify the result,
check that the new string fits in the output buffer, copy the contents
to the new string to the output buffer, discard the new string, and
finally, when the entire format string has been processed, resize the
output buffer to the right size.

however, the memory allocator is fast, block copies are cheap, and
operator access is faster than global function accesses, so as long
as the format string is short, the resulting string is no more than 100
bytes longer, and Raymond hasn't gotten around to optimize the str()
code path, % can be slightly faster than a plain call to str(). in all
other cases, str() is faster.

(using either on the output from glob.glob is just plain silly, of course)

</F>



 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: [OT] Re: output from external commands Terry Hancock Python 2 10-26-2005 12:53 AM
[OT] Re: output from external commands darren kirby Python 2 10-25-2005 03:42 AM
output from external commands James Colannino Python 2 10-24-2005 05:27 AM
capturing the output of external commands Avi Kak Python 4 07-26-2004 03:16 AM
Re: man pages for C commands (GCC commands) Ben Pfaff C Programming 4 06-28-2003 06:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57