首页 > 解决方案 > Python startswith 不匹配相同的字符串

问题描述

所以,如果我在首先定义变量列表的函数中使用代码块,我可以运行代码,但是当我将变量传递给另一个函数并运行startswith()时,它返回false!

def lambda_handler(event, context): 

    test = ['2', 'Snapshot-Only2019-10-07T06-08-43.522172Dev/']
    name_of_snapshot_folder = test[1]
    total_instances = int(test[0])

    print('name_of_snapshot_folder : {0}'.format(name_of_snapshot_folder))

    start_status_check(name_of_snapshot_folder, total_instances) 

def start_status_check(name_of_snapshot_folder, total_instances): 

    try: 
        print('Type of total instance inside start status check : {0}'.format(type(total_instances)))
        snap_list = [] 
        count = 0 
        print('Type of snapshot folder is :{0}'.format(type(name_of_snapshot_folder)))
        print('Total instances are :{0}'.format(total_instances))
        client = boto3.client('s3')
        new_name = str(name_of_snapshot_folder).strip()
        print('type pf new name: {0}'.format(type(new_name)))

        for i in my_bucket.objects.all():
            print('Printing i inside my bucket loop: {0}'.format(i.key))
            print('Snapshot-Only2019-10-07T06-08-43.522172Dev/ff'.startswith(new_name))

            if(i.key.startswith(new_name) and i.key.endswith('.txt')): 
                count += 1 
                print(count)
                print('Printing i :{0}'.format(i))
        print("count value {0}, true or false is? {1} :".format(count, count == total_instances))
        if count == total_instances: 
            print('testing phase') 
            #client = boto3.client('lambda') 
            print('Total files matches total instances, invoking status check function') 
            check_backup_status(total_instances, name_of_snapshot_folder) 
        else: 
            print('Inside codepipeline continued execution') 

startswith() 应该返回 True,但是它在第二个函数中返回 false

标签: pythonpython-3.xaws-lambda

解决方案


strip() 完成了这项工作!修复如此简单


推荐阅读