在python中向值附加特殊字符

2024-05-02 18:00:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试删除那些环境(实例)被终止的所有堆栈。在将堆栈\u名称传递给delete\u堆栈时,它抛出了一个错误。我尝试将特殊字符(如“”)添加到堆栈名称的值中,但没有成功。有人能帮我解决这个问题吗。提前谢谢!你知道吗

#!/usr/bin/env python

import boto
import boto.ec2
import boto.cloudformation
import re
from datetime import datetime, timedelta

utclast = datetime.utcnow() - timedelta(2)

conn = boto.cloudformation.connect_to_region('us-west-1',aws_access_key_id = '<access_key>',aws_secret_access_key = '<secret_key>')
conn_ec2 = boto.ec2.connect_to_region('us-west-1',aws_access_key_id = '<access_key>',aws_secret_access_key = '<secret_key>')

stacks = conn.list_stacks()
for stackSumm in stacks:
    pattern = re.compile("Testupload-env([a-zA-Z0-9]+)")
    match = pattern.match(stackSumm.stack_name)
    if stackSumm.stack_status in "CREATE_COMPLETE" and match and stackSumm.stack_name in match.string:
        m = re.split(r'Testupload-', stackSumm.stack_name)
        instance = conn_ec2.get_all_instances(filters={"tag:Name": m[1]})
        if not instance:

            try:
                    print "Trying to delete stack: %s" % stackSumm.stack_name
                    conn.delete_stack(stackSumm.stack_name)

            except boto.exception.BotoServerError, e:
                    print e.error_message

错误:

File "delete_stack.py", line 7, in <module>
    conn.delete_stack(Testupload-envmeraleb8b01739116b0f36d17a2b5445b949f592bb625-6293)
NameError: name 'Testupload' is not defined

Tags: keynameinimportawssecretaccessstack
1条回答
网友
1楼 · 发布于 2024-05-02 18:00:59

所有发布的代码都没有生成错误,但NameError表示您没有提供有效的堆栈名称

delete_stack(stack_name_or_id) Deletes a specified stack. Once the call completes successfully, stack deletion starts. Deleted stacks do not show up in the DescribeStacks API if the deletion has been completed successfully.

Parameters: stack_name_or_id (string) – The name or the unique identifier associated with the stack.

名称/id应该是字符串,python中的字符串必须用单引号或双引号引起来

stack_name = "Testupload-envmeraleb8b01739116b0f36d17a2b5445b949f592bb625-6293"
conn.delete_stack(stack_name)

相关问题 更多 >