-5

Say there's a variable

x = "/real/path/"

inside a text file "setting.txt", and in a config xml file "config.xml" contains a tag

<root>    
    <setting>setting.txt</setting> 
    ......
</root>

In my Python script test.py, I would like to do this:

import xml.etree.ElementTree as ET
import os

x = "/path/placeholder"    

def load_global_setting_from_config_xml(in_xml):
    tree = ET.parse(in_xml)
    root = tree.getroot()
    config_node = root.find("setting")
    if config_node is not None:
        config_file_path = config_node.text
        if os.path.exists(config_file_path):
           execfile(config_file_path) # looks not working

load_global_setting_from_config_xml("config.xml")
print(x) #expected output is /real/path

it looks execfile() does not working.

Dracarys
  • 291
  • 1
  • 11

1 Answers1

0

As a solution for original execfile(), From this answer, if modify the one line:

execfile(config_file_path)

to

execfile(config_file_path, globals())

then it will work as expected.

Dracarys
  • 291
  • 1
  • 11
  • Why use global? I think it would be better to pass arguments and return a function result. –  Jul 30 '18 at 08:45
  • @intentionallyleftblank , it's a legacy issue, the setting.txt in the real case may not be as clean as I posted here. – Dracarys Jul 30 '18 at 09:36