473,395 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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?
Jul 18 '05 #1
8 2732
"Neale" <nh*****@ieee.org> wrote in message
news:2d**************************@posting.google.c om...
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?


Jul 18 '05 #2
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 <mi*********@zoran.com>
The only difference between children and adults is the price of the toys.

Jul 18 '05 #3
"Miki Tebeka" <mi*********@zoran.com> wrote in message
news:ma*************************************@pytho n.org...
Hello PaxDiablo,
[[snip]]

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

Bye.
--
-------------------------------------------------------------------------
Miki Tebeka <mi*********@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.
Jul 18 '05 #4
> > > [[snip]]
How about :
===
files = os.listdir(".")
===

Bye.
--
-------------------------------------------------------------------------
Miki Tebeka <mi*********@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?
Jul 18 '05 #5
On 2004-06-14, Neale <nh*****@ieee.org> wrote:
> > [[snip]]
> How about :
> ===
> files = os.listdir(".")
> ===
>
> Bye.
> --
> -------------------------------------------------------------------------
> Miki Tebeka <mi*********@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

Jul 18 '05 #6
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
Jul 18 '05 #7

"Neale" <nh*****@ieee.org> wrote in message
news:2d**************************@posting.google.c om...
> [[snip]]
How about :
===
files = os.listdir(".")
===

Bye.
--

-------------------------------------------------------------------------
Miki Tebeka <mi*********@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.
Jul 18 '05 #8
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)

Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mohsin | last post by:
Hi all, I have a perl program which makes a user exit to the O/S (unix, solaris) to issue a O/S command. I know that the shell it invokes is NOT a korn shell, because I captured the shell info...
1
by: dk | last post by:
is there any way to invoke a unix shell script from .net? tia, dk
0
by: Aashif | last post by:
I want to call Unix Shell script which is available in other Server (Unix server) from windows application using C#. Currently the shell script runs the C program but the GUI is not good, So I want...
9
by: sohan | last post by:
Hi, I want to know how to connect and execute a db2 query from inside a UNIX shell script. Details: We have a unix shell script. We need to execute multiple db2 sql queries from this shell...
3
by: Deniz Dogan | last post by:
Hello. I was thinking about writing a UNIX shell program using Python. Has anyone got any experience on this? Is it even possible? I have programmed a simple shell in C before and I came to...
0
by: skyineyes | last post by:
Hi Can anybody provide information on accessing DB2 database through UNIX shell scripts. Any tutorial will ne highly appreciated. Thanks
21
by: Tom Gur | last post by:
Hi, It's seems that csh and tcsh acts a bit different when handling special characters in quotes. i.e: if i'll supply my program with the following arguments: -winpath "c:\\temp\\" tcsh will...
2
by: gagandutta01 | last post by:
Hi, Can anyone tell me how to execute a function declared in Oracle Package from Unix shell script? I created a shell script and after connecting to oracle database i am using exec @...
3
by: regnumber | last post by:
Hi. I am new to Unix. I need to write a Unix Shell Script to extract records from the table and write those extracted datas to a text file. DB using is DB2. Can anyone give me some sample...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.