简体   繁体   English

无法从 Python 中的函数返回值

[英]Can't return value from function in Python

I'm trying to return a value called 'html' from a python function:我正在尝试从 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

But python isn't seeing the value html when I try to return it.但是当我尝试返回它时,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

Why is python not seeing this value called html?为什么python没有看到这个叫做html的值? How can I return it?我怎样才能退货?

html doesn't exist when interactive==1 .interactive==1html不存在。

Depending on what you want to do, you might want to create html before the if else statement根据您要执行的操作,您可能希望在if else语句之前创建html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM