Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将配置文件路径作为变量传递到函数中_Python_Configuration - Fatal编程技术网

Python 将配置文件路径作为变量传递到函数中

Python 将配置文件路径作为变量传递到函数中,python,configuration,Python,Configuration,我正在读取项目文件夹中本地存储的配置文件。如果在fileconfig.read('C:/fileconfig.ini')中硬编码配置文件的位置,则不存在任何问题。但是,当我在同一个函数fileconfig.read(path)中传递file的path变量时,它给出了空值 请告诉我如何在函数fileconfig.read(path)中提供文件路径 Python代码: def read_config_from_file(path): fileconfig = configparser.Con

我正在读取项目文件夹中本地存储的配置文件。如果在
fileconfig.read('C:/fileconfig.ini')
中硬编码配置文件的位置,则不存在任何问题。但是,当我在同一个函数
fileconfig.read(path)
中传递file的path变量时,它给出了空值

请告诉我如何在函数
fileconfig.read(path)
中提供文件路径

Python代码:

def read_config_from_file(path):
    fileconfig = configparser.ConfigParser()
    # fileconfig.read('C:/FileConfig.ini') # works perfectly
    fileconfig.read(path)                  # Empty Value
    configfromFileDict = dict()
    for section in fileconfig.sections():
        # configfromFileDict[section] = {}
        for option in fileconfig.options(section):
            print(option, fileconfig.get(section, option))
            configfromFileDict[option] = fileconfig.get(section,option)

    return configfromFileDict

configurations = read_config_from_file('C:/FileConfig.ini')

你能展示一下你是如何从文件中调用
read\u config\u的吗?
?添加了调用功能这应该可以正常工作。是的,添加一个导入和一个ini文件,代码就可以工作了。python 3.7.3