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

Jason Orendorff jason at jorendorff.com
Tue Jan 15 11:18:20 EST 2002


Heiko Wolf wrote:
> Hi,
> 
> I've written a python script that does something like that:
> 
> 
> #! /usr/bin/python
> 
> import os
> os.system("/usr/bin/python script_file") #call python with script_file
> as input
> 
> 
> the script file shall use some user_defined commands that are defined
> in a module my_module.py.
> 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.
> 
> Is there a python-function that does this?

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")

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list