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

Execute commands from file

Hello,

Thanks for your time.

We have very big files with python commands (more or less, 500000
commands each file).

It is possible to execute them command by command, like if the
commands was typed one after the other in a interactive session?

( Better using command flags than with an small script like "while 1:
input()" )

Thanks a lot.

May 16 '07 #1
15 2768
In <11*********************@u30g2000hsc.googlegroups. com>, tmp123 wrote:
We have very big files with python commands (more or less, 500000
commands each file).

It is possible to execute them command by command, like if the
commands was typed one after the other in a interactive session?
Take a look at the `code` module in the standard library:

In [31]: code?
Type: module
Base Class: <type 'module'>
String Form: <module 'code' from '/usr/lib/python2.4/code.pyc'>
Namespace: Interactive
File: /usr/lib/python2.4/code.py
Docstring:
Utilities needed to emulate Python's interactive interpreter.

Ciao,
Marc 'BlackJack' Rintsch
May 16 '07 #2
tmp123 wrote:
Hello,

Thanks for your time.

We have very big files with python commands (more or less, 500000
commands each file).
Those are BIG programs. Presumably other programs are writing them?
It is possible to execute them command by command, like if the
commands was typed one after the other in a interactive session?
You need to look for "pdb", the interactive Python debugger. This is
capable of single-step operations, and supports breakpoints.
( Better using command flags than with an small script like "while 1:
input()" )

Thanks a lot.
You are pretty much going to have to run pdb then trigger your code by
calling a pdb method with a function in your code as an argument, if I
am remembering correctly how it works.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

May 16 '07 #3
"tmp123" schrieb >
We have very big files with python commands
(more or less, 500000 commands each file).

It is possible to execute them command by command,
inp = open(cmd_file)
for line in inp:
exec line

might help. You don't get quite the same feeling as
"like if the commands was typed one after the other
in a interactive session", but perhaps this helps.

Warning: the code above is without any error checks.
You might also run into security problems, the example
above assumes you trust your input.

HTH. YMMV.
Martin


May 16 '07 #4
Martin Blume wrote:
"tmp123" schrieb >
>We have very big files with python commands
(more or less, 500000 commands each file).

It is possible to execute them command by command,

inp = open(cmd_file)
for line in inp:
exec line

might help. You don't get quite the same feeling as
"like if the commands was typed one after the other
in a interactive session", but perhaps this helps.

Warning: the code above is without any error checks.
You might also run into security problems, the example
above assumes you trust your input.

HTH. YMMV.
Martin
The problem with this approach is that each line executes without any
connection to the environment created by previous lies.

Try it on a file that reads something like

xxx = 42
print xxx

and you will see NameError raised because the assignment hasn't affected
the environment for the print statement.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

May 16 '07 #5
On May 16, 1:05 pm, Steve Holden <s...@holdenweb.comwrote:
Martin Blume wrote:
"tmp123" schrieb >
We have very big files with python commands
(more or less, 500000 commands each file).
It is possible to execute them command by command,
inp = open(cmd_file)
for line in inp:
exec line
might help. You don't get quite the same feeling as
"like if the commands was typed one after the other
in a interactive session", but perhaps this helps.
Warning: the code above is without any error checks.
You might also run into security problems, the example
above assumes you trust your input.
HTH. YMMV.
Martin

The problem with this approach is that each line executes without any
connection to the environment created by previous lies.

Try it on a file that reads something like

xxx = 42
print xxx

and you will see NameError raised because the assignment hasn't affected
the environment for the print statement.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------
cat file:

x = 100
print x

cat file.py:
#!/usr/bin/python2.4

import os.path
import sys

file, ext = os.path.splitext(sys.argv[0])
f = open(file,'rb')
for i in f:
exec i
>./file.py
100

Don't see the problem though.

May 17 '07 #6
On Thu, 17 May 2007 00:30:23, i3dmaster <i3*******@gmail.comwrote
>f = open(file,'rb')
for i in f:
exec i
Why are you opening the file in binary mode?

--
Doug Woodrow

May 17 '07 #7
i3dmaster wrote:
On May 16, 1:05 pm, Steve Holden <s...@holdenweb.comwrote:
>Martin Blume wrote:
>>"tmp123" schrieb >
We have very big files with python commands
(more or less, 500000 commands each file).
It is possible to execute them command by command,
inp = open(cmd_file)
for line in inp:
exec line
might help. You don't get quite the same feeling as
"like if the commands was typed one after the other
in a interactive session", but perhaps this helps.
Warning: the code above is without any error checks.
You might also run into security problems, the example
above assumes you trust your input.
HTH. YMMV.
Martin
The problem with this approach is that each line executes without any
connection to the environment created by previous lies.

Try it on a file that reads something like

xxx = 42
print xxx

and you will see NameError raised because the assignment hasn't affected
the environment for the print statement.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

cat file:

x = 100
print x

cat file.py:
#!/usr/bin/python2.4

import os.path
import sys

file, ext = os.path.splitext(sys.argv[0])
f = open(file,'rb')
for i in f:
exec i
>./file.py
100

Don't see the problem though.
No, because there isn't one. Now try adding a function definition and
see how well it works.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

May 17 '07 #8
Steve Holden a écrit :
i3dmaster wrote:
>On May 16, 1:05 pm, Steve Holden <s...@holdenweb.comwrote:
>>Martin Blume wrote:
"tmp123" schrieb >
We have very big files with python commands
(more or less, 500000 commands each file).
It is possible to execute them command by command,
inp = open(cmd_file)
for line in inp:
exec line
The problem with this approach is that each line executes without any
connection to the environment created by previous lies.

Try it on a file that reads something like

xxx = 42
print xxx
cat file:

x = 100
print x

cat file.py:
#!/usr/bin/python2.4

import os.path
import sys

file, ext = os.path.splitext(sys.argv[0])
f = open(file,'rb')
for i in f:
exec i
>>./file.py
100

Don't see the problem though.
No, because there isn't one. Now try adding a function definition and
see how well it works.

regards
Steve
This is just a problem with indentation and blocks of code, the
followong will do :

commands = open("commands")
namespace, block = {}, ""
for line in commands :
line=line[:-1]
if not line : continue
if line[0].isspace() :
block += '\n' + line
continue
else :
if block.strip() :
exec block in namespace
block = line

exec block in namespace
print dict((k, v) for k, v in namespace.items() if k != "__builtins__")
with commands containing :

"""

x = 5

def toto(arg) :
print arg

def inner() :
print arg*arg

inner()
toto(x)
"""

output :
5
25
{'x': 5, 'toto': <function toto at 0x01D30C70>}

(sorry Steve for the private mail)

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 4 26 88 00 97
Mobile: +33 6 32 77 00 21

May 17 '07 #9
"Steve Holden" schrieb
>
Try it on a file that reads something like

xxx = 42
print xxx

and you will see NameError raised because the assignment
hasn't affected the environment for the print statement.
[...]
No, because there isn't one. Now try adding a function
definition and see how well it works.
C:\temp>more question.py
xxx=42
print xxx
def sowhat():
print xxx

print xxx
C:\temp>c:\programme\python\python
Python 2.4 (#60, Nov 30 2004, 11:49:19)
[MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license"
for more information.
>>exec open("question.py").read()
42
42
>>sowhat()
42
>>xxx
42
Seems to work great to me.

OTOH, this doesn't:
>>inp=open("question.py")
for l in inp:
.... exec l
....
42
Traceback (most recent call last):
File "<stdin>", line 2, in ?
File "<string>", line 1
def sowhat():
^
SyntaxError: unexpected EOF while parsing
So it seems to depend on the way the file is read.
Regards
Martin

May 17 '07 #10
On May 17, 3:02 am, Douglas Woodrow <newsgro...@nospam.demon.co.uk>
wrote:
On Thu, 17 May 2007 00:30:23, i3dmaster <i3dmas...@gmail.comwrote
f = open(file,'rb')
for i in f:
exec i

Why are you opening the file in binary mode?

--
Doug Woodrow
'b' is generally useful on systems that don't treat binary and text
files differently. It will improve portability.

May 17 '07 #11
Martin Blume wrote:
"Steve Holden" schrieb
>>>Try it on a file that reads something like

xxx = 42
print xxx

and you will see NameError raised because the assignment
hasn't affected the environment for the print statement.

[...]
No, because there isn't one. Now try adding a function
definition and see how well it works.
C:\temp>more question.py
xxx=42
print xxx
def sowhat():
print xxx

print xxx
C:\temp>c:\programme\python\python
Python 2.4 (#60, Nov 30 2004, 11:49:19)
[MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license"
for more information.
>>>exec open("question.py").read()
42
42
>>>sowhat()
42
>>>xxx
42
Seems to work great to me.

OTOH, this doesn't:
>>>inp=open("question.py")
for l in inp:
... exec l
...
42
Traceback (most recent call last):
File "<stdin>", line 2, in ?
File "<string>", line 1
def sowhat():
^
SyntaxError: unexpected EOF while parsing
So it seems to depend on the way the file is read.
It depends on the way the lines of the file are executed, not how they
are read. And you may remember the original poster was proposing this:

inp = open(cmd_file)
for line in inp:
exec line
As for your first example, why not just use execfile() ?

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

May 17 '07 #12
On Fri, 18 May 2007 04:45:30, Dennis Lee Bieber <wl*****@ix.netcom.com>
wrote
>On 17 May 2007 13:12:10 -0700, i3dmaster <i3*******@gmail.comdeclaimed
the following in comp.lang.python:
>'b' is generally useful on systems that don't treat binary and text
files differently. It will improve portability.

"b" is needed for binary files on systems that /do/ treat binary
differently from text. And it does add to portability only in that it
has no effect on those that treat all files the same.

However, as I recall the thread, the intent is to process text lines
from a file -- and using "b" is going to affect how the line endings are
being treated.
Yes that was my understanding too, Dennis, and the reason I queried it
in the first place. I had to remove the "b" option in order to get the
sample code to work under Windows, because the standard line termination
under Windows is carriage return + linefeed (\r\n).

Of course if I manually edit the command file so that it only has a
linefeed character at the end of each line, the binary mode works.

So I think i3dmaster's method is only portable as long as the command
file is created with unix-style line termination.

--
Doug Woodrow

May 18 '07 #13
"Steve Holden" schrieb

[ difference between exec open(fname).read()
and for line in open(fname): exec line ]

So it seems to depend on the way the file is read.
It depends on the way the lines of the file are executed,
not how they are read.
Could you elaborate a little bit more on the difference?
I assumed that because read() reads the whole file, the
body of my function sowhat() is present, so that it can
be parsed while the invocation of exec is still running.
If it is read and exec'd line by line, the definition of
the function is still left open at the moment exec() ends,
causing the "EOF" error. Hence my statement, "it depends
on the way the file is read".

And you may remember the original poster was
proposing this:

inp = open(cmd_file)
for line in inp:
exec line

As for your first example, why not just use execfile() ?
I assume that
execfile(fname)
is equivalent to
exec open(fname).read() ?
Regards
Martin
May 19 '07 #14
Martin Blume wrote:
"Steve Holden" schrieb
>>[ difference between exec open(fname).read()
and for line in open(fname): exec line ]

So it seems to depend on the way the file is read.
It depends on the way the lines of the file are executed,
not how they are read.
Could you elaborate a little bit more on the difference?
I assumed that because read() reads the whole file, the
body of my function sowhat() is present, so that it can
be parsed while the invocation of exec is still running.
If it is read and exec'd line by line, the definition of
the function is still left open at the moment exec() ends,
causing the "EOF" error. Hence my statement, "it depends
on the way the file is read".
I simply meant that the whole source has to be presented to the exec
statement and not chunked into lines.

Clearly I could read all the source in with

lines = open(cmd_file).readlines()

but if you then proceed to try and execute the source line by line as in

for l in lines:
exec l

you will hit problems because of the disjoint nature of the execution
which will breal up indented suites and so on.

I was probably just a little over-zealous in pursuing correct English
usage, in which case please accept my apology.
>
>And you may remember the original poster was
proposing this:

inp = open(cmd_file)
for line in inp:
exec line

As for your first example, why not just use execfile() ?
I assume that
execfile(fname)
is equivalent to
exec open(fname).read() ?
Pretty much.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

May 19 '07 #15
"Steve Holden" schrieb
>
I simply meant that the whole source has to be presented
to the exec statement and not chunked into lines.
That's what I meant: With exec open(f).read() it is not
broken into several exec invocations.
>
I was probably just a little over-zealous in pursuing
correct English usage, in which case please accept
my apology.
The apology is on my part, I didn't explain my thinking
clearly enough.
Thanks for your explanations. Makes my newbie understanding
of Python much more robust.

Regards
Martin
May 19 '07 #16

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

Similar topics

3
by: Tomasz Ludwiniak | last post by:
Hi, I've little problem... I try to execute asp file ( which return XML file ) and use the result it in my asp script : set xml = Server.CreateObject("MSXML2.DOMDocument") xml.async=false...
15
by: Madhanmohan S | last post by:
Hi All, I want to run a command line appplication from C#. When i start the application, it will go into specific mode. After that, i have to give commands to use the application. How Can This Be...
1
by: Faraz | last post by:
Hey guys. How cam I interface with the environment in C#. I want to execute commands in a (separate) process, just like java Runtime.exec(string command) does. Thanks in advance,
2
by: Big Santini | last post by:
Question: how to 'execute' different file types? in example when user choose ..doc file, the deafult application for .doc should run. jaro
15
by: dylpkls91 | last post by:
I have been researching this topic and come up with some code to make it work. It uses SSL and requires the 3rd party package Paramiko (which requires PyCrypto). However, at this moment I have no...
13
by: nickyeng | last post by:
I have the apache files in my directory: /home/Nick/apache_1.3.33 I have files : Install README apache apache.exe
2
by: mayurshah01 | last post by:
Hey guyz, I'm leaning c++. i wanna execute *.exe file using c++ pogramming. For example I have four *.exe files, a.exe b.exe c.ece d.exe Now i'll ask user for option that which file he/she...
1
by: Hishaam | last post by:
How to execute commands in internal zones of solaris using python running from the global zone ? i tried -- 1os.popen("zlogin <zone1>") 2os.popen("zonename") the 2nd command executes back...
4
by: pizzetta72 | last post by:
Hi all, i just finish to install cygwin on windows XP adding also gcc compiler module. After I tried to execute a file called fstsg.0.3-sparc (www.fstha.com) used to cript a Unix shell, I receive...
1
by: nthato | last post by:
Hi I am trying to create a script that will execute commands on several remote servers and I am having problems to increment my positional variables. I the a simple way of doing this???
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.