Can';t在Python中从函数返回值
我试图从python函数返回一个名为“html”的值:Can';t在Python中从函数返回值,python,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 outp
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
但是当我试图返回html时,python没有看到它的值
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
回溯(最近一次呼叫最后一次):
文件“\aws\u ec2\u list\u instances\u no\u output.py”,第657行,在
main()
文件“\aws\u ec2\u list\u instances\u no\u output.py”,主目录第631行
html、html文件、输出文件、输出文件名、删除html文件=循环帐户(aws环境列表、交互式、字段名、所有帐户)
文件“\aws\u ec2\u list\u instances\u no\u output.py”,第594行,循环内账户
返回html、html文件、输出文件、输出文件名、删除html文件
UnboundLocalError:分配前引用的局部变量“html”
为什么python没有看到这个称为html的值?如何返回它?
html
在interactive==1
时不存在
根据您要执行的操作,您可能希望在
if-else
语句之前创建html
如果interactive==1
html
在回溯时不存在,我感觉错误来自其他地方,即这一部分:返回html、htmlfile、output\u文件、output\u文件名、remove\u htmlfile
,这与您的函数完全不同。好的,如果我在函数开始时将html
设置为None
,如果interactive==1,当我在的else中设置html
时,我会返回它的实际值吗?是!您可以在else
语句中修改变量html
。相反,如果输入if
语句,函数将返回None
。