开发者

How to change the file name runtime

I have imported a file using the below command

code='IN'
exec "import %s_tmp_file" % code

Now i want to use this same imported file for other operations. How to give the file name in that case?

str = code_开发者_开发百科tmp_file.dict # this does not work.


Use the __import__ builtin instead of exec:

my_module = __import__("%s_tmp_file" % code)
str = my_module.dict

See this page or help(__import__) for more information.


Use __import__ (docs).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜