HowTo exec every line of a file inside python program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe Richett

    HowTo exec every line of a file inside python program

    Hi all,

    I have a python program and I have some "user defined" command stored in
    a separate text file. Is there a builtin command with which I can load
    the "user defined"-file and execute every command line by line, e.g.

    #
    # MAIN PROGRAM
    #

    def a(i):
    print "here we are #", i

    if __name__ == "__main__":
    for i in range(10):
    a(i)
    <execute everything from FILE.PY>

    ------------------------------------------------------

    #
    # USER DEFINED FILE
    #
    print "here we are in the user defined file"


  • Peter Otten

    #2
    Re: HowTo exec every line of a file inside python program

    Joe Richett wrote:
    [color=blue]
    > Hi all,
    >
    > I have a python program and I have some "user defined" command stored in
    > a separate text file. Is there a builtin command with which I can load
    > the "user defined"-file and execute every command line by line, e.g.
    >
    > #
    > # MAIN PROGRAM
    > #
    >
    > def a(i):
    > print "here we are #", i
    >
    > if __name__ == "__main__":
    > for i in range(10):
    > a(i)
    > <execute everything from FILE.PY>
    >
    > ------------------------------------------------------
    >
    > #
    > # USER DEFINED FILE
    > #
    > print "here we are in the user defined file"[/color]

    import user_defined_fi le

    will do it (once) assuming the file is named user_defined_fi le.py and is in
    the python path, e. g. in the current directory. However, it is better to
    wrap the code in user_defined_fi le.py into a function to make exceution
    independent from import:

    import user_defined_fi le
    # more code
    user_defined_fi le.myfunc()

    Please read Python's excellent tutorial - it's a good starting point and has
    some info on how to organize your code, too.

    Peter

    Comment

    • Tim Hochberg

      #3
      Re: HowTo exec every line of a file inside python program

      Joe Richett wrote:
      [color=blue]
      > Hi all,
      >
      > I have a python program and I have some "user defined" command stored in
      > a separate text file. Is there a builtin command with which I can load
      > the "user defined"-file and execute every command line by line, e.g.[/color]

      Look up execfile under built-in functions is the docs.

      -tim



      [color=blue]
      >
      > #
      > # MAIN PROGRAM
      > #
      >
      > def a(i):
      > print "here we are #", i
      >
      > if __name__ == "__main__":
      > for i in range(10):
      > a(i)
      > <execute everything from FILE.PY>
      >
      > ------------------------------------------------------
      >
      > #
      > # USER DEFINED FILE
      > #
      > print "here we are in the user defined file"
      >
      >[/color]


      Comment

      Working...