Execute commands from file

Martin Blume mblume at socha.net
Sat May 19 07:38:29 EDT 2007


"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
   




More information about the Python-list mailing list