How to Access Unix Shell

Discussion in 'Python' started by Neale, Jun 14, 2004.

  1. Neale

    Neale Guest

    In a python program, I want to do a Unix directory list and then
    append selected files to each other which, as you know, is just "cat
    filename2 >> filename1." How do I escape into the shell and then
    return, bringing with me a directory list?

    Where can I find some examples of mixing shell commands and Python?
     
    Neale, Jun 14, 2004
    #1
    1. Advertising

  2. Neale

    PaxDiablo Guest

    "Neale" <> wrote in message
    news:...
    > In a python program, I want to do a Unix directory list and then
    > append selected files to each other which, as you know, is just "cat
    > filename2 >> filename1." How do I escape into the shell and then
    > return, bringing with me a directory list?



    How about?
    =====
    import os
    pipeLine = os.popen("ls -1","r")
    curLine = pipeLine.readline()
    while curLine:
    print "Got file [%s]"%(curLine[:-1])
    curLine = pipeLine.readline()
    pipeLine.close()
    =====


    >
    > Where can I find some examples of mixing shell commands and Python?
     
    PaxDiablo, Jun 14, 2004
    #2
    1. Advertising

  3. Neale

    Miki Tebeka Guest

    Hello PaxDiablo,

    > How about?
    > =====
    > import os
    > pipeLine = os.popen("ls -1","r")
    > curLine = pipeLine.readline()
    > while curLine:
    > print "Got file [%s]"%(curLine[:-1])
    > curLine = pipeLine.readline()
    > pipeLine.close()
    > =====

    How about :
    ===
    files = os.listdir(".")
    ===

    Bye.
    --
    -------------------------------------------------------------------------
    Miki Tebeka <>
    The only difference between children and adults is the price of the toys.
     
    Miki Tebeka, Jun 14, 2004
    #3
  4. Neale

    PaxDiablo Guest

    "Miki Tebeka" <> wrote in message
    news:...
    > Hello PaxDiablo,
    >
    > > [[snip]]

    > How about :
    > ===
    > files = os.listdir(".")
    > ===
    >
    > Bye.
    > --
    > -------------------------------------------------------------------------
    > Miki Tebeka <>
    > The only difference between children and adults is the price of the toys.
    >


    Well, I could claim that I knew about listdir but was simply answering the
    original question which was how to run arbitrary shell commands.

    But in fact I knew nothing of listdir (all of my coding so far has been with
    wx(Python|Widgets) so I rarely use the python system stuff) , so I thank you
    for the education :).

    Pax.
     
    PaxDiablo, Jun 14, 2004
    #4
  5. Neale

    Neale Guest

    > > > [[snip]]
    > > How about :
    > > ===
    > > files = os.listdir(".")
    > > ===
    > >
    > > Bye.
    > > --
    > > -------------------------------------------------------------------------
    > > Miki Tebeka <>

    >
    > Well, I could claim that I knew about listdir but was simply answering the
    > original question which was how to run arbitrary shell commands.
    >
    > But in fact I knew nothing of listdir (all of my coding so far has been with
    > wx(Python|Widgets) so I rarely use the python system stuff) , so I thank you
    > for the education :).
    >
    > Pax.

    Thank you for such high quality help.

    If there are other Unix command functions like listdir, where/how do I
    find them? Is there a particular directory in Python, maybe?
     
    Neale, Jun 14, 2004
    #5
  6. Neale

    Lee Harr Guest

    On 2004-06-14, Neale <> wrote:
    >> > > [[snip]]
    >> > How about :
    >> > ===
    >> > files = os.listdir(".")
    >> > ===
    >> >
    >> > Bye.
    >> > --
    >> > -------------------------------------------------------------------------
    >> > Miki Tebeka <>

    >>
    >> Well, I could claim that I knew about listdir but was simply answering the
    >> original question which was how to run arbitrary shell commands.
    >>
    >> But in fact I knew nothing of listdir (all of my coding so far has been with
    >> wx(Python|Widgets) so I rarely use the python system stuff) , so I thank you
    >> for the education :).
    >>
    >> Pax.

    > Thank you for such high quality help.
    >
    > If there are other Unix command functions like listdir, where/how do I
    > find them? Is there a particular directory in Python, maybe?



    http://python.org/
    http://python.org/doc/
    http://docs.python.org/modindex.html
    http://docs.python.org/lib/module-os.html
    http://docs.python.org/lib/module-shutil.html
     
    Lee Harr, Jun 14, 2004
    #6
  7. Neale wrote:

    > In a python program, I want to do a Unix directory list and then
    > append selected files to each other which, as you know, is just "cat
    > filename2 >> filename1." How do I escape into the shell and then
    > return, bringing with me a directory list?
    >
    > Where can I find some examples of mixing shell commands and Python?


    [~]> ipython
    Python 2.2.3 (#1, Oct 15 2003, 23:33:35)
    Type "copyright", "credits" or "license" for more information.

    IPython 0.6.1.cvs -- An enhanced Interactive Python.
    ? -> Introduction to IPython's features.
    @magic -> Information about IPython's 'magic' @ functions.
    help -> Python's own help system.
    object? -> Details about 'object'. ?object also works, ?? prints more.

    In [1]: cd test
    /usr/local/home/fperez/test

    In [2]: ls
    argv.py* die.py div.py err.pyc image2.eps ipython.log strings.py
    t.py
    bar.py div.c error.py* exit.py* image.eps ramptest.py* times.sh*
    bar.pyc div.f err.py foo.py image.ps scopes.py* tmp/

    In [3]: cp t.py tmp/
    cp: overwrite `tmp/t.py'? y

    In [4]: cd tmp
    /usr/local/home/fperez/test/tmp

    In [5]: ll
    total 8
    -rw-r--r-- 1 fperez wavelet 91 Jun 15 00:19 strings.py
    -rw-r--r-- 1 fperez wavelet 43 Jun 15 00:27 t.py

    In [6]: cat strings.py >> t.py

    In [7]: ll
    total 8
    -rw-r--r-- 1 fperez wavelet 91 Jun 15 00:19 strings.py
    -rw-r--r-- 1 fperez wavelet 134 Jun 15 00:27 t.py

    In [8]: !!ls
    Out[8]: ['strings.py', 't.py']

    In [9]: for fname in _8:
    ...: print 'filename:',fname
    ...:
    filename: strings.py
    filename: t.py


    Hope this helps,

    f
     
    Fernando Perez, Jun 15, 2004
    #7
  8. Neale

    Paul Watson Guest

    "Neale" <> wrote in message
    news:...
    > > > > [[snip]]
    > > > How about :
    > > > ===
    > > > files = os.listdir(".")
    > > > ===
    > > >
    > > > Bye.
    > > > --

    > >

    > -------------------------------------------------------------------------
    > > > Miki Tebeka <>

    > >
    > > Well, I could claim that I knew about listdir but was simply answering

    the
    > > original question which was how to run arbitrary shell commands.
    > >
    > > But in fact I knew nothing of listdir (all of my coding so far has been

    with
    > > wx(Python|Widgets) so I rarely use the python system stuff) , so I thank

    you
    > > for the education :).
    > >
    > > Pax.

    > Thank you for such high quality help.
    >
    > If there are other Unix command functions like listdir, where/how do I
    > find them? Is there a particular directory in Python, maybe?


    You could look at all of the items in the os module. The module directory
    at http://docs.python.org/modindex.html might also be good material.

    Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
    win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> dir(os)

    ['F_OK', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT',
    'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', '
    O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH',
    'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAI
    T', 'R_OK', 'TMP_MAX', 'UserDict', 'W_OK', 'X_OK', '_Environ', '__all__',
    '__builtins__', '__doc__', '__file__', '__name
    __', '_copy_reg', '_execvpe', '_exists', '_exit', '_get_exports_list',
    '_make_stat_result', '_make_statvfs_result', '_pi
    ckle_stat_result', '_pickle_statvfs_result', 'abort', 'access', 'altsep',
    'chdir', 'chmod', 'close', 'curdir', 'defpath'
    , 'dup', 'dup2', 'environ', 'error', 'execl', 'execle', 'execlp', 'execlpe',
    'execv', 'execve', 'execvp', 'execvpe', 'ex
    tsep', 'fdopen', 'fstat', 'fsync', 'getcwd', 'getcwdu', 'getenv', 'getpid',
    'isatty', 'linesep', 'listdir', 'lseek', 'ls
    tat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep',
    'pipe', 'popen', 'popen2', 'popen3', 'popen4', '
    putenv', 'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir',
    'sep', 'spawnl', 'spawnle', 'spawnv', 'spawnve',
    'startfile', 'stat', 'stat_float_times', 'stat_result', 'statvfs_result',
    'strerror', 'sys', 'system', 'tempnam', 'times
    ', 'tmpfile', 'tmpnam', 'umask', 'unlink', 'unsetenv', 'utime', 'waitpid',
    'walk', 'write']
    >>> print os.listdir.__doc__

    listdir(path) -> list_of_strings

    Return a list containing the names of the entries in the directory.

    path: path of directory to list

    The list is in arbitrary order. It does not include the special
    entries '.' and '..' even if they are present in the directory.
     
    Paul Watson, Jun 15, 2004
    #8
  9. Paul Watson wrote:

    > Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
    > win32
    > Type "help", "copyright", "credits" or "license" for more information.
    >
    >>>>import os
    >>>>dir(os)


    What about :
    Python 2.3.3 (#2, Feb 24 2004, 09:29:20)
    [GCC 3.3.3 (Debian)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> help(os)
     
    =?ISO-8859-1?Q?Gr=E9goire_Dooms?=, Jun 15, 2004
    #9
    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. Replies:
    2
    Views:
    5,731
    Jonathan Bromley
    Feb 18, 2005
  2. Christian Heimes
    Replies:
    0
    Views:
    606
    Christian Heimes
    Feb 27, 2008
  3. Gerardo Herzig
    Replies:
    1
    Views:
    1,090
    Philipp Pagel
    Feb 27, 2008
  4. D'Arcy J.M. Cain
    Replies:
    0
    Views:
    869
    D'Arcy J.M. Cain
    Feb 27, 2008
  5. Samuel A. Falvo II
    Replies:
    0
    Views:
    487
    Samuel A. Falvo II
    Oct 8, 2008
Loading...

Share This Page