473,396 Members | 1,757 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,396 software developers and data experts.

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"
Jul 18 '05 #1
2 1466
Joe Richett wrote:
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"


import user_defined_file

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

import user_defined_file
# more code
user_defined_file.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
Jul 18 '05 #2
Joe Richett wrote:
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.
Look up execfile under built-in functions is the docs.

-tim


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

Jul 18 '05 #3

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

Similar topics

2
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
3
by: Mike C. Fletcher | last post by:
Every few months I get to working on a Python C extension (built with distutils) and discover a pointer error or the like where I'd like to be able to step into my DLL and see where the extension...
4
by: Logan | last post by:
Several people asked me for the following HOWTO, so I decided to post it here (though it is still very 'alpha' and might contain many (?) mistakes; didn't test what I wrote, but wrote it - more or...
5
by: Nick Jacobson | last post by:
This works fine: x = 1 def execfunc(): print x execfunc() So why doesn't this? s = \
8
by: R. Bernstein | last post by:
In doing the extension to the python debugger which I have here: http://sourceforge.net/project/showfiles.php?group_id=61395&package_id=175827 I came across one little thing that it would be nice...
5
by: TPJ | last post by:
I have the following code: ----------------------------------- def f(): def g(): a = 'a' # marked line 1 exec 'a = "b"' in globals(), locals() print "g: a =", a
4
by: carl.dhalluin | last post by:
Hello I am completely puzzled why the following exec code does not work: mycode = "import math\ndef f(y):\n print math.floor(y)\nf(3.14)" def execute(): exec mycode execute()
26
by: warth33 | last post by:
Hello I have a php site. Some page needs to call an external program. The programs are home made c# applications. It uses to work without problem. For a while. Maybe it work for some hour....
7
by: gregory.lielens | last post by:
Hi, I am using a small python file as an input file (defining constants, parameters, input data, ...) for a python application. The input file is simply read by an exec statement in a specific...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.