当前位置: 首页 > 后端技术 > Python

execfile和exec的区别

时间:2023-03-25 23:02:30 Python

有时候想把一个文件的内容赋值给一个变量,可以用execfile和execexecfilepy3及以上版本好像不支持,可以用exec代替例子:test.py:importostest_path=os.path.dirname(__file__)config_path=os.path.join(os.path.split(test_path)[0],'config')defparse_conf(conf_file,variable_scope):exec(open(os.path.join(config_path,conf_file)).read(),variable_scope)variable_scope.pop('__builtins__',None)log_config={'module':'test',}parse_conf("log.conf",log_config)print(log_config)log.conf内容:level="INFO"log_root='./log'project='test'https://docs.python.org/3.3/w...