How to Access Unix Shell

N

Neale

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?
 
P

PaxDiablo

Neale said:
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()
=====
 
M

Miki Tebeka

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.
 
P

PaxDiablo

Miki Tebeka said:
Hello PaxDiablo,
How about :
===
files = os.listdir(".")
===

Bye.

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.
 
N

Neale

[[snip]]
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?
 
L

Lee Harr

[[snip]]
How about :
===
files = os.listdir(".")
===

Bye.

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
 
F

Fernando Perez

Neale said:
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
 
P

Paul Watson

Neale said:
[[snip]]
How about :
===
files = os.listdir(".")
===

Bye.
--
-------------------------------------------------------------------------

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.['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']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.
 
?

=?ISO-8859-1?Q?Gr=E9goire_Dooms?=

Paul said:
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.

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top