无法从Python中的函数返回值

时间:2019-12-02 15:24:44

标签: python

我正在尝试从python函数返回一个名为“ html”的值:

def loop_accounts(account_name, interactive): 
    regions = set_regions(account_name)
    aws_env_list = os.path.join('..', '..', 'output_files', 'account_names_list', 'aws_kiki_page-' + today + '.csv')
    # Set the output file
    output_dir = os.path.join('..', '..', 'output_files', 'aws_instance_list', 'csv', '')
    if interactive == 1:
        output_file = os.path.join(output_dir, 'aws-instance-list-' + account_name + '-' + today +'.csv')
        output_file_name = 'aws-instance-list-' + account_name + '-' + today + '.csv'
    else:
        output_file = os.path.join(output_dir, 'aws-instance-master-list-' + today +'.csv')
        output_file_name = 'aws-instance-master-list-' + today +'.csv'
        htmlfile, htmlfile_name, remove_htmlfile = convert_csv_to_html_table(output_file, today, interactive, account_name)
        with open(htmlfile, 'r') as htmlfile:
            html = htmlfile.read()
    return html

但是当我尝试返回它时,python没有看到html值。

Traceback (most recent call last):
  File ".\aws_ec2_list_instances_no_output.py", line 657, in <module>
    main()
  File ".\aws_ec2_list_instances_no_output.py", line 631, in main
    html, htmlfile, output_file, output_file_name, remove_htmlfile = loop_accounts(aws_env_list, interactive, fieldnames, all_accounts_question)
  File ".\aws_ec2_list_instances_no_output.py", line 594, in loop_accounts
    return html, htmlfile, output_file, output_file_name, remove_htmlfile
UnboundLocalError: local variable 'html' referenced before assignment

为什么python没有看到名为html的值?我该如何退货?

1 个答案:

答案 0 :(得分:2)

db.test.aggregate([ { $match: { $and: [ {type: {$in: ["TOYS"]}} ] } } ]) html中不存在。

根据您要执行的操作,您可能想在interactive==1语句之前创建html