f



How to Access Unix Shell

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?
0
nhirsh2 (6)
6/14/2004 12:48:03 AM
comp.lang.python 77058 articles. 6 followers. Post Follow

8 Replies
609 Views

Similar Articles

[PageSpeed] 33

"Neale" <nhirsh2@ieee.org> wrote in message
news:2d7af4f8.0406131648.42868c63@posting.google.com...
> 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?



0
nobody (5159)
6/14/2004 3:58:50 AM
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 <miki.tebeka@zoran.com>
The only difference between children and adults is the price of the toys.

0
miki.tebeka1 (253)
6/14/2004 7:35:01 AM
"Miki Tebeka" <miki.tebeka@zoran.com> wrote in message
news:mailman.929.1087194935.6949.python-list@python.org...
> Hello PaxDiablo,
>
> > [[snip]]
> How about :
> ===
> files = os.listdir(".")
> ===
>
> Bye.
> --
> -------------------------------------------------------------------------
> Miki Tebeka <miki.tebeka@zoran.com>
> 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.


0
nobody (5159)
6/14/2004 8:38:25 AM
> > > [[snip]]
> > How about :
> > ===
> > files = os.listdir(".")
> > ===
> >
> > Bye.
> > --
> > -------------------------------------------------------------------------
> > Miki Tebeka <miki.tebeka@zoran.com>
> 
> 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?
0
nhirsh2 (6)
6/14/2004 2:15:12 PM
On 2004-06-14, Neale <nhirsh2@ieee.org> wrote:
>> > > [[snip]]
>> > How about :
>> > ===
>> > files = os.listdir(".")
>> > ===
>> >
>> > Bye.
>> > --
>> > -------------------------------------------------------------------------
>> > Miki Tebeka <miki.tebeka@zoran.com>
>> 
>> 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

0
missive (110)
6/14/2004 7:15:57 PM
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
0
fperez528 (156)
6/15/2004 6:28:45 AM
"Neale" <nhirsh2@ieee.org> wrote in message
news:2d7af4f8.0406140615.310f1875@posting.google.com...
> > > > [[snip]]
> > > How about :
> > > ===
> > > files = os.listdir(".")
> > > ===
> > >
> > > Bye.
> > > --
> >
> -------------------------------------------------------------------------
> > > Miki Tebeka <miki.tebeka@zoran.com>
> >
> > 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.


0
pwatson2903 (133)
6/15/2004 2:11:39 PM
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)
0
dooms1 (18)
6/15/2004 3:33:08 PM
Reply: