Function that enters a line at the beginning of a file?

Steve Holden sholden at holdenweb.com
Tue Jan 15 14:48:20 EST 2002


"Alex Martelli" <aleax at aleax.it> wrote in message
news:a21p2u$did$1 at serv1.iunet.it...
> "Jason Orendorff" <jason at jorendorff.com> wrote in message
> news:mailman.1011111801.11359.python-list at python.org...
>     ...
> > Maybe you want this.  It avoids using os.system() too, so it should
> > be faster.  I hope it's self-explanatory.
> >
> >   #!/usr/bin/python
> >   exec "import my_module"
> >   execfile("script_file")
>
> I think I must be missing something.  Why
>     exec "import my_module"
> rather than the plain good old
>     import my_module
> ...?

Because the OP said:

> Now I want to open script_file and shift the whole text one line down,
> and enter in the top line "import my_module", so that the commands are
> available without having to enter the import-line myself.

which Jason took to mean a requirement for importing modules with names not
known a priori?

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list