473,398 Members | 2,427 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,398 software developers and data experts.

Is pyton for me?


I need to write simple scripts for executing command line functions. Up till now I've used C-Shell scripts for this, but I'm looking for a better alternative. And I keep reading about how “easy” it is to program with python.

Unfortunately after reading "diveintopython" and the python tutorial, I'm not sure how to do what I need to do.

Here is an example of the type of thing I would like to be able to do. Can I do this with python? How do I get python to execute command line functions?
From the command prompt (not from the python interpreter – running linux for example) I would type:


gensky 3 21 12 > sky.rad

This would call the non-python program “gensky” which creates a computer model of a sky on march 21st at 12:00 PM and puts this information into a file called sky.rad.

If I create a CSH script like the one below, I can automate tasks by building loops along the lines of:

#!/bin/csh -fv

# simple script to create multiple sky files.

foreach hour (10 12 14)
gensky 3 21 $hour > sky$hour.rad
end

If I try and do a gensky command from the python interpreter or within a python.py file, I get an error message:

NameError: name ‘gensky’ is not defined

So even if I get the python syntax correct, I can’t get these commands to execute.

If anyone has any suggestions on how to get python scripts to execute this sort of thing, what I should be looking at, or if there is something else I might consider besides python, please let me know.

Thanks for your help.

Mark

_______________________________________________
Join Excite! - http://www.excite.com
The most personalized portal on the Web!
Jul 19 '05 #1
11 1986
Mark de+la+Fuente wrote:
I need to write simple scripts for executing command line functions. Up till
now I've used C-Shell scripts for this, but I'm looking for a better
alternative. And I keep reading about how "easy" it is to program with
python.

Unfortunately after reading "diveintopython" and the python tutorial, I'm not
sure how to do what I need to do.

Here is an example of the type of thing I would like to be able to do.
Can I do this with python? How do I get python to execute command line
functions?


import os
os.system('gensky 3 21 12 > sky.rad')

Jul 19 '05 #2
Mark de la Fuente wrote:
Here is an example of the type of thing I would like to be able to do.
Can I do this with python? How do I get python to execute command line
functions? ... # simple script to create multiple sky files.

foreach hour (10 12 14)
gensky 3 21 $hour > sky$hour.rad
end
Dan Bishop gave one example using os.system. The important
thing to know is that in the shell all programs can be used
as commands while in Python there isn't a direct connection.
Instead you need to call a function which translates a
request into something which calls the command-line program.

There are several ways to do that. In Python before 2.4
the easiest way is with os.system(), which takes the command-line
text as a string. For example,

import os
os.system("gensky 3 21 10 > sky10.rad")

You could turn this into a Python function rather easily

import os

def gensky(hour):
os.system("gensky 3 21 %d > sky%d.rad" % (hour, hour))

for hour in (10, 12, 14):
gensky(hour)
Python 2.4 introduces the subprocess module which makes it
so much easier to avoid nearly all the mistakes that can
occur in using os.system(). You could replace the 'gensky'
python function with

import subprocess
def gensky(hour):
subprocess.check_call(["gensky", "3", "21", str(hour)],
stdout = open("sky%d.rad" % (hour,), "w"))
The main differences here are:
- the original code didn't check the return value of os.system().
It should do this because, for example, the gensky program might
not be on the path. The check_call does that test for me.

- I needed to do the redirection myself. (I wonder if the
subprocess module should allow

if isinstance(stdout, basestring):
stdout = open(stdout, "wb")

Hmmm....)

If I try and do a gensky command from the python interpreter or within a
python.py file, I get an error message:

NameError: name ‘gensky’ is not defined
That's because Python isn't set up to search the command path
for an executable. It only knows about variable names defined
in the given Python module or imported from another Python
module.
If anyone has any suggestions on how to get python scripts to execute
this sort of thing, what I should be looking at, or if there is
something else I might consider besides python, please let me know.


You'll have to remember that Python is not a shell programming
language. Though you might try IPython - it allows some of the
things you're looking for, though not all.

You should also read through the tutorial document on Python.org
and look at some of the Python Cookbook.. Actually, start with
http://wiki.python.org/moin/BeginnersGuide

Andrew
da***@dalkescientific.com

Jul 19 '05 #3
On Thu, 9 Jun 2005 22:50:20 -0400 (EDT), "Mark de+la+Fuente"
<de****@excite.com> declaimed the following in comp.lang.python:


foreach hour (10 12 14)
gensky 3 21 $hour > sky$hour.rad
end

import os

for hour in [10, 12, 14]:
os.system("gensky 3 21 %s > sky%s.rad" % (hour, hour))
Python is a stand-alone language. External programs have to be
explicitly invoked.
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 19 '05 #4
Andrew Dalke wrote:
import subprocess
def gensky(hour):
subprocess.check_call(["gensky", "3", "21", str(hour)],
stdout = open("sky%d.rad" % (hour,), "w"))
The main differences here are:
- the original code didn't check the return value of os.system().
It should do this because, for example, the gensky program might
not be on the path. The check_call does that test for me.


Where do you find check_call()? It's not in the docs and I get
import subprocess
subprocess.check_call

Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'module' object has no attribute 'check_call'

with Python 2.4.1.

Kent
Jul 19 '05 #5
Kent Johnson wrote:
Where do you find check_call()? It's not in the docs and I get
>>> import subprocess
>>> subprocess.check_call

Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'module' object has no attribute 'check_call'

with Python 2.4.1.


Interesting. I got my subprocess.py from CVS. The CVS log

revision 1.12
date: 2005/01/01 09:36:34; author: astrand; state: Exp; lines: +39 -1
New subprocess utility function: check_call. Closes #1071764.

The bug tracker is
http://sourceforge.net/tracker/index...70&atid=305470

which says it's a 2.5ism. Oops! Sorry about that post
from the future. I didn't realize it.

Andrew
da***@dalkescientific.com

Jul 19 '05 #6
"Mark de+la+Fuente" <de****@excite.com> writes:
I need to write simple scripts for executing command line functions.
Up till now I've used C-Shell scripts for this, but I'm looking for
a better alternative. And I keep reading about how “easy” it is to
program with python.


Lots of answers - but everyone concentrated on telling you how to do
what you were trying to do, and no one answered your question. Which
is actually typical of the c.l.python, but may not be what you want.

As others pointed out, Python isn't a shell, or even a shell scripting
language, so it doesn't handle what you're doing in a "natural"
way. Because of that, it may not be the language for you. Python has
features that work well for building large systems, but those tend to
cause extra work when you want to do things that other languages make
simple.

One thing you might consider is changing shells. See <URL:
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ > for reasons why
you might want to drop csh. There's a similar paper discussing the
problems with later versions of sh (bash, late ksh, etc.), but from
what I recall the problems mostly come from trying to use the extra
features that have been added to those shells. So when writing sh
scripts, I tend to stay with fundamental bourne features.

Other languages have less overhead in invoking external commands. Rexx
and Perl come to mind. You might want to give them a look.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #7
Honestly, I don't think that Python is for you. Python is an object
programming language, and you are looking for simple automation tool,
in order to avoid programming. If shell scripting is no good enough, try
tcl.

DG

Jul 19 '05 #8
On Fri, 10 Jun 2005 17:50:15 -0500, Mike Meyer <mw*@mired.org> declaimed
the following in comp.lang.python:

Other languages have less overhead in invoking external commands. Rexx
and Perl come to mind. You might want to give them a look.
Especially Rexx -- since any "statement" that is not recognized
as a part of the Rexx language will be automatically kicked out to the
currently addressed command host (typically a shell). Though one may
need to be wary of Rexx's tendency to uppercase everything <G>
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 19 '05 #9
Mike Meyer <mw*@mired.org> writes:
"Mark de+la+Fuente" <de****@excite.com> writes:
I need to write simple scripts for executing command line functions.
Up till now I've used C-Shell scripts for this, but I'm looking for
a better alternative. And I keep reading about how “easy” it is to
program with python.
As others pointed out, Python isn't a shell, or even a shell scripting
language, so it doesn't handle what you're doing in a "natural"
way. Because of that, it may not be the language for you. Python has
features that work well for building large systems, but those tend to
cause extra work when you want to do things that other languages make
simple.


I agree with this approach. For me, there has to be a certain level of
complexity before I reach for python as a tool. This usually involves
one or more of the following:

1: text processing involving multiple files. For example, running
statistics on file A, looking up values stored in file B.

2: cases where the overhead of repeatedly opening processes and pipes
becomes unacceptable.
For example:
for f in file/*; do
cp $f $f.bak
sed -e 'something' < $f.bak > $f
done

This works well with small numbers of files. But even though sed is
quicker than python, starting a new sed process with every iteration
quickly stacks up. Rewriting the entire thing to run as a single
process can dramatically improve performance. Although this might be a
case of premature optimization.

3: cases where figuring out how to do something using one of the POSIX
shell utilities makes my head hurt.

Personally, I hate popen and avoid using it when possible. There is
nothing wrong with sh as a glue language when all you need is something
like: grep text file | filter | filter > output_file.
<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.


--
Kirk Job-Sluder
"The square-jawed homunculi of Tommy Hilfinger ads make every day an
existential holocaust." --Scary Go Round
Jul 19 '05 #10
Kirk Job Sluder wrote:
I agree with this approach. For me, there has to be a certain level of
complexity before I reach for python as a tool.


For me as well. In my case, the key question is "is this bash script
going to be longer than two lines?". If it would be, the Python
approach will be done faster and will be more maintainable, since I
often don't look at those scripts again for months and would tend to
forget any bash syntax that I've learned in the meantime.

:-)

-Peter
Jul 19 '05 #11
Peter Hansen <pe***@engcorp.com> writes:
Kirk Job Sluder wrote:
I agree with this approach. For me, there has to be a certain level of
complexity before I reach for python as a tool.


For me as well. In my case, the key question is "is this bash script
going to be longer than two lines?". If it would be, the Python
approach will be done faster and will be more maintainable, since I
often don't look at those scripts again for months and would tend to
forget any bash syntax that I've learned in the meantime.


I tend to switch from sh to python when I start writing a loop. A long
list of repeated commands - even with conditionals - isn't to bad. But
when you start forking processes in a loop, it's time to use a more
powerful tool.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #12

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

Similar topics

6
by: Stephen VanDahm | last post by:
I'm looking for a way to install Python on a UNIX machine in a way such that any user on the system can use it, but only to execute scripts that are located in a certain directory. I do not have...
2
by: Steffen Kunze | last post by:
Hello I am from Germany and I study Information-Technology (2nd semester). Every three month I have a internship at the DLR (German Aerospace Center) and in the moment I try to use Python and...
1
by: Philippe C. Martin | last post by:
Hi, I'm looking for a very simple way to figure out if a line of code requires further lines of code ex: for i in range(10): So far I have found the following trick that seem dangerous to me...
3
by: mike | last post by:
Hi, I am new with python. Is it possible to have an MFC application and develop some module using python? what are the steps in doing this? can anybody give me a url or some documentation for...
27
by: psbasha | last post by:
Hi, I want to create a Exe of an Pyton Aplication in Windows and UNIX.For example,Ihave the following ".py"files Sample1.py Sample2.py Sample3.py ................ Samplen.py Main.py
4
bgeddy
by: bgeddy | last post by:
Hi to all. I have been spending a lot of time trying out various combinations of tools/libraries etc with a view to developing applications for Linux (KDE so preferably QT based) with a Mysql...
0
by: mmomar | last post by:
Hi, I am using a HTTPS connection to invoke a cgi-script. I want to use a timeout between the sending the request and receiving the response, so what I want to do is when the request is send,...
1
by: nbrdja | last post by:
Hi everyone :) (telit 862-GPS) pyton script Is there anyone have solution how to use GPIO line on 862-GPS to act as "interupt"? for example: i'd like to send sms or turn on LED (immiditly)...
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
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.