1

This is probably quite a simple problem, but can't seem to figure it out or find any answered questions with regards to this issue.

xcombined=[]
for i in range (1,20):
    from 'files_'+str(i) import x
    for j in range(0,len(x)):
        xcombined.append(x[j])

results in the following error:

    import "files_"+str(i)
                  ^
SyntaxError: invalid syntax

I am wishing to import a list, (ie x=[15.34, ..., 15.54]) from files_1.py, files_2.py, etc and append it to another list called xcombined so that xcombined is sum of all of the "x's" from all the "files_*.py's". If I am correct 'files_'+str(i) cannot work as it isn't a string? Is there a quick way around this problem?

Thanks in advance. Paul

2 Answers2

1

Use importlib module here, as import statement doesn't work with strings.

import importlib
xcombined=[]
for i in range (1,20):
    mod = importlib.import_module('files_'+str(i))
    x = mod.x  # fetch `x` from the imported module
    del mod    # now delete module, so this is equivalent to: `from mod import x`
    for j in range(0,len(x)):
        xcombined.append(x[j])

importlib was introduced in py2.7, for earlier versions use __import__.

help on __import__:

>>> print __import__.__doc__
__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module

Import a module. Because this function is meant for use by the Python
interpreter and not for general use it is better to use
importlib.import_module() to programmatically import a module.
...
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
0

From what I've understood, you're using .py files to store lists as python code.

This is extremely ineffective and will probably cause many problems. It's better to use simple text files (for exemple in the JSON format).

Still, here's how you should import modules with dynamically generated names:

import importlib
i = 1
myModule = importlib.import_module('files_'+str(i))
halflings
  • 1,540
  • 1
  • 13
  • 34