Re: output from external commands

Discussion in 'Python' started by darren kirby, Oct 24, 2005.

  1. darren kirby

    darren kirby Guest

    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-----
     
    darren kirby, Oct 24, 2005
    #1
    1. Advertising

  2. darren kirby

    Kent Johnson Guest

    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
     
    Kent Johnson, Oct 24, 2005
    #2
    1. Advertising

  3. darren kirby

    Mike Meyer Guest

    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.
     
    Mike Meyer, Oct 24, 2005
    #3
  4. darren kirby

    Peter Hansen Guest

    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
     
    Peter Hansen, Oct 24, 2005
    #4
  5. 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
     
    James Colannino, Oct 24, 2005
    #5
  6. 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
     
    Terry Hancock, Oct 24, 2005
    #6
  7. 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>
     
    Fredrik Lundh, Oct 24, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Pfaff

    Re: man pages for C commands (GCC commands)

    Ben Pfaff, Jun 24, 2003, in forum: C Programming
    Replies:
    4
    Views:
    3,995
    Thomas Stegen
    Jun 28, 2003
  2. Avi Kak
    Replies:
    4
    Views:
    440
    Fernando Perez
    Jul 26, 2004
  3. James Colannino

    output from external commands

    James Colannino, Oct 24, 2005, in forum: Python
    Replies:
    2
    Views:
    299
    James Colannino
    Oct 24, 2005
  4. darren kirby

    [OT] Re: output from external commands

    darren kirby, Oct 25, 2005, in forum: Python
    Replies:
    2
    Views:
    343
    Tony Meyer
    Oct 25, 2005
  5. Terry Hancock

    Re: [OT] Re: output from external commands

    Terry Hancock, Oct 25, 2005, in forum: Python
    Replies:
    2
    Views:
    284
    Bengt Richter
    Oct 26, 2005
Loading...

Share This Page