Member Avatar for DancingDana

Hello!

I've got this collection of python files in a directory. Each file contains a variable called 'ors' that has as its value a dictionary.

I know you can get the value of the variable in a file by typing:
>>> import filename
>>> filename.variable

What I'd like to do is open the variables in all the files and add the contents to one big dictionary.
I tried printing the values of the variables by using glob and then typing:

for filename in globbed_dir:
    print filename.ors

but all I get is an error message saying there is no module called 'filename'. How can I get Python to interpret 'filename' as a variable and open all the ors's in the files?

I've wondered that too, but I think it can be done with eval

for filename in dir:
    eval("import %s" % filename)
    # dosomething
Member Avatar for DancingDana

Hello ultimatebuster!
Thanks for your reply.

Either I'm doing something wrong, or eval is not doing what we want it to do, but I get this error message when I try using it:

Traceback (most recent call last):
File "<pyshell#21>", line 5, in <module>
eval("import %s" % filename)
File "<string>", line 1
import a
^
SyntaxError: invalid syntax

('a' is the name of the file)

Thanks for mentioning eval() though. It led me to execfile(), which seems to do the trick:

for i in f:
    execfile(i)

If you then typ the name of the variable in the file:
>>> ors
it returns the value as specified in the file.

Member Avatar for DancingDana

Hold on..
It works when I typ it directly in the shell, but when I write it in a script I get an error message:
NameError: global name 'ors' is not defined

how do I get past this?

"filename" should contain the complete path, not just the file name.

Member Avatar for DancingDana

Thanks for your reply woooee, but it's still not working:

for filename in globbed_dir:
	eval("import %s" % filename)

Traceback (most recent call last):
File "<pyshell#74>", line 2, in <module>
eval("import %s" % filename)
File "<string>", line 1
import C:\Python26\CorpusPJ\wikis\eng_wiki.py
^
SyntaxError: invalid syntax

What am I doing wrong? ...

Alternatively chdir to directory before import. Your must user __import__() function not the statement.

However it is little strange that you would need to do what you are asking. Could you maybe introduce the function of your program in rough pseudo code so maybe we could find you better alternative way of organizing things.

import os
os.chdir('/Tests')
tdir = [py[:-3] for py in os.listdir(os.curdir) if py.endswith('.py')]
print tdir

for i in tdir:
    __import__(i)
Member Avatar for DancingDana

My apologies ultimatebuster. I got it working with eval, although in a slightly different way:

from f_dir import *
import glob

globbed_dir = glob.glob('path/to/dir/*')
for filename in globbed_dir:
    ors = eval("%s.ors" % filename)

Thanks for your reply tonyjv!
Maybe it is weird what I'm trying to do, but I only started learning Python a few months ago. I'll try to explain why I want to do this, and if you know of a better way to do it, I'm all ears!

I'm trying to build a certain tool. Before using it, the user needs to set the values of certain variables in a file. Depending on the value of one of these variables, I need to gather the contents of several files in some directory.
As explained before, those files are .py files and contain a variable 'ors' with a dictionary as their value.
I can't simply write the names of the files I want to extract these variables from in the script, because I don't know beforehand which ones I will need to open. That depends on the choices of the user.
So I glob the dir, take out the files I don't need, and read the variables in the other files.
The information is in separate files because it's quite a lot of data.

I hope this makes sense..

I tried you code, but I get an error. Maybe I'm doing something wrong:

os.chdir('path/to/dir')
tdir = [py[:-3] for py in os.listdir(os.curdir) if py.endswith('.py')]
print tdit
for i in tdir:
    __import__(i)

Traceback (most recent call last):
File "<pyshell#8>", line 2, in <module>
__import__(i)
ImportError: No module named file1

More sure way is by appending the os.curdir to path to make sure it is there:

import os,sys
os.chdir('/test')
sys.path.append(os.curdir)
tdir = [py[:-3] for py in os.listdir(os.curdir) if py.endswith('.py')]
print tdir

for i in tdir:
    __import__(i)

Those data you are collecting, have you thought picling those? How much is quite a bit of data? 100's of kBytes, Mbytes or Gbytes?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.