Can I import an arbitary file?

Fernando Pérez fperez528 at yahoo.com
Mon Feb 25 14:44:41 EST 2002


Peter Ballard wrote:

> I've written a program (an SRAM generator, if you must know) which
> has some configuration info (for different SRAM sizes etc.) in a separate
> file. This configuration file is python code.
> 
> What I want to do is import this file. The problem is, this file could
> be any name at all. What I want to do is parse my command line, and
> find out the name of my configuration file, and then import the file of
> that name. Is that possible?

look at __import__
 
> (Obviously I can read in this arbitrary file and parse it,
> but to actually execute it would be much neater).
> 
> Or is there any other way I can read in a file of an arbitary name,
> and execute its code?

look at execfile





More information about the Python-list mailing list