473,732 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2746
"Neale" <nh*****@ieee.o rg> wrote in message
news:2d******** *************** ***@posting.goo gle.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.readli ne()
while curLine:
print "Got file [%s]"%(curLine[:-1])
curLine = pipeLine.readli ne()
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.readli ne()
while curLine:
print "Got file [%s]"%(curLine[:-1])
curLine = pipeLine.readli ne()
pipeLine.close( )
=====

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

Bye.
--
-------------------------------------------------------------------------
Miki Tebeka <mi*********@zo ran.com>
The only difference between children and adults is the price of the toys.

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

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

Bye.
--
-------------------------------------------------------------------------
Miki Tebeka <mi*********@zo ran.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|Widge ts) 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*********@zo ran.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|Widge ts) 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.o rg> wrote:
> > [[snip]]
> How about :
> ===
> files = os.listdir(".")
> ===
>
> Bye.
> --
> -------------------------------------------------------------------------
> Miki Tebeka <mi*********@zo ran.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|Widge ts) 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:',fna me
...:
filename: strings.py
filename: t.py
Hope this helps,

f
Jul 18 '05 #7

"Neale" <nh*****@ieee.o rg> wrote in message
news:2d******** *************** ***@posting.goo gle.com...
> [[snip]]
How about :
===
files = os.listdir(".")
===

Bye.
--

-------------------------------------------------------------------------
Miki Tebeka <mi*********@zo ran.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|Widge ts) 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_l ist',
'_make_stat_res ult', '_make_statvfs_ result', '_pi
ckle_stat_resul t', '_pickle_statvf s_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_tim es', 'stat_result', 'statvfs_result ',
'strerror', 'sys', 'system', 'tempnam', 'times
', 'tmpfile', 'tmpnam', 'umask', 'unlink', 'unsetenv', 'utime', 'waitpid',
'walk', 'write'] print os.listdir.__do c__

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
5667
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 into a file with a 'ps' command. My question is "How to explicitly specify a Korn shell to be used by perl?" Eg of my perl code: ## Begin code snippet..
1
2062
by: dk | last post by:
is there any way to invoke a unix shell script from .net? tia, dk
0
3057
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 to create GUI in C# windows application and call that C program using Shell script so first I have to call unix shell script from C#. Please guide me friends.
9
78367
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 script and export the result to a file. Any code snippet on this will be helpful.
3
1778
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 think about how perfect Python would be for parsing user input. Regards, Deniz Dogan
0
1246
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
3027
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 take it as -winpath "c:\temp\" and csh will take it literally (with the double-slashes). Is there a way for me to know what shell is currently running my
2
10294
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 @ Db_name.Packagename.function_name('p1','P2'); and after executing the shell scripts i am getting the follwing error:-ERROR at line 13: ORA-06550: line 13, column 2: PLS-00221: 'function_name' is not a procedure or is undefined
3
4436
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 shell script thanks
0
8774
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9181
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.