Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.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 如何使用Argparse打开和读取属性文件_Python_Json_Python 3.x_Python 2.7_Properties File - Fatal编程技术网

Python 如何使用Argparse打开和读取属性文件

Python 如何使用Argparse打开和读取属性文件,python,json,python-3.x,python-2.7,properties-file,Python,Json,Python 3.x,Python 2.7,Properties File,我被这个python程序卡住了,它使用Argparse参数读取文件。不知何故,在读取属性文件(application-DEV.properties)时,它给了我一个错误“没有这样的文件和目录”。但是我可以在代码库中看到该文件。下面是我的代码 要执行的命令: python create_param_store.py --Tier DEV Python代码: def load_envproperties(sep='=', comment_char='#'): props = {}

我被这个python程序卡住了,它使用Argparse参数读取文件。不知何故,在读取属性文件(
application-DEV.properties
)时,它给了我一个错误“没有这样的文件和目录”。但是我可以在代码库中看到该文件。下面是我的代码

要执行的命令:

python create_param_store.py --Tier DEV
Python代码:

def load_envproperties(sep='=', comment_char='#'):
    
    props = {}
    with open("../../../src/main/resources/application.properties",'rt') as f:
        e=set(f.readlines())
        
    with open("'../../../src/main/resources/application-' +  pa.Tier + '.properties'", 'rt') as f:
        d=set(f.readlines())
        for line in list(d-e):
            l = line.strip()
            if l and not l.startswith(comment_char):
                key_value = l.split(sep)
                key = key_value[0].strip()
                value = sep.join(key_value[1:]).strip().strip('"') 
                props[key] = value
                print(props[key])
                putssm(Name=key, Value=props[key])
            
        
def init():
    global pa
    parser = argparse.ArgumentParser(description='Additional Params.')
    parser.add_argument('--Tier', nargs='?', dest='Tier', required=False, help='Tier')
    pa = parser.parse_args()
错误:

 File "create_param_store.py", line 59, in main
    load_envproperties()
  File "create_param_store.py", line 38, in load_envproperties
    with open("'../../../src/main/resources/application-' +  pa.Tier + '.properties'", 'rt') as f:
FileNotFoundError: [Errno 2] No such file or directory: "'../../../src/main/resources/application-' +  pa.Tier + '.properties'"

[Container] 2020/08/30 21:37:42 Command did not exit successfully python create_param_store.py --Tier $Tier exit status 1
[Container] 2020/08/30 21:37:42 Phase complete: BUILD State: FAILED

这是一个输入错误,您的单引号周围有双引号,但仍然希望里面的代码正常工作。使用一种引号、正确转义或使用f字符串。谢谢Grismar。我试图按定义进行更改,但这次它给出了语法错误
文件“create_param_store.py”,第38行的open(''../../../src/main/resources/application-'+pa.Tier+'.properties'','rt')为f:^SyntaxError:无效语法
该字符串中仍然存在引号问题。为什么开头和结尾都有
(一个双引号)?我建议使用一个编辑器或IDE来帮助避免这些非常基本的问题(例如拼写错误、错误编写语言的基本语法),例如PyCharm、Syder或VSCode(仅举几例)。除了引号问题之外,还有硬编码目录
。/../src/main/resources/
将相对于进程的当前工作目录。这真的是您的意图吗?还是您打算使用相对于脚本位置的路径?提示:包含脚本的目录将是
os.path.dirname(\uu文件\uu)