如何在我的python脚本中解决访问问题

时间:2019-04-22 12:09:50

标签: python-2.7

我希望对变量进行全局访问并在函数中进行设置。

def parse_command_line_arg(properties_section_name, properties_file,log_file):  
    print("Entering parse_command_line_arg: ")
    parse = argparse.ArgumentParser()
    parse.add_argument("-s")
    parse.add_argument("-p")
    parse.add_argument("-l")
    args = parse.parse_args()
    properties_section_name=args.s
    properties_file=args.p
    log_file=args.l
    print("properties_section_name: " + properties_section_name + ". properties_file: " + properties_file + ".log_file: " + log_file)  

    try:
        does_log_file_exist=os.path.isfile(log_file)
        does_prop_file_exist=os.path.isfile(properties_file)
        if properties_section_name is None:
            raise Exception("Properties section name cannot be empty")
        if not does_log_file_exist :
            raise Exception("Log File " + log_file + " doesnot exist")
        if not does_prop_file_exist :
            raise Exception("Properties File " + properties_file + " doesnot exist")
    except Exception as e:
        print(e)
        raise
    finally:
        print("Exiting function parse_command_line_arg")

我尝试放置打印语句,并检查了传递的变量名。无济于事。我想念什么?

1 个答案:

答案 0 :(得分:0)

向函数添加全局变量可以解决此问题。但是想知道这是否是一种好的编码习惯吗?