使用python排除s3中的多个路径

使用python排除s3中的多个路径,python,Python,错误: 使用一个路径可以工作,但我需要使用多个路径。您的代码正在测试元组是否为字符串。这行不通 请参见下面的代码 from boto3 import client exclude_paths=('DC','A') conn = client('s3') for key in conn.list_objects(Bucket='my-bucket')['Contents']: if exclude_paths not in key['Key']: print

错误:


使用一个路径可以工作,但我需要使用多个路径。

您的代码正在测试元组是否为字符串。这行不通

请参见下面的代码

from boto3 import client
exclude_paths=('DC','A')
conn = client('s3') 

for key in conn.list_objects(Bucket='my-bucket')['Contents']:  
       if exclude_paths not in key['Key']:
         print(key['Key'])

您的代码正在测试元组是否为字符串。这行不通

请参见下面的代码

from boto3 import client
exclude_paths=('DC','A')
conn = client('s3') 

for key in conn.list_objects(Bucket='my-bucket')['Contents']:  
       if exclude_paths not in key['Key']:
         print(key['Key'])

排除路径
是元组,而不是字符串

所以不是你的

from boto3 import client
exclude_paths=('DC','A')
conn = client('s3') 

for key in conn.list_objects(Bucket='my-bucket')['Contents']:  
       if key['Key'] not in exclude_paths:
         print(key['Key'])
使用


增编:

如果要打印排除路径中不以字符串开头的路径,请使用

   if key['Key'] not in exclude_paths:

而不是原始的
if
语句。

排除路径
是一个元组,而不是字符串

所以不是你的

from boto3 import client
exclude_paths=('DC','A')
conn = client('s3') 

for key in conn.list_objects(Bucket='my-bucket')['Contents']:  
       if key['Key'] not in exclude_paths:
         print(key['Key'])
使用


增编:

如果要打印排除路径中不以字符串开头的路径,请使用

   if key['Key'] not in exclude_paths:

而不是你原来的
if
语句。

是的,我试过了,但没有得到我想要的结果。它给我带来了这个:A/A/B/DC/DC/LastAWRReport-200607-103252.html DC/LastAWRReport-200607-104438.html DC/LastAWRReport-200607-123731.html DC/hs_err_pid1003103.log DC/hs_err_pid257708.log DC/hs_err_pid367738.log test/test/test/terraform/terraform.tfstate test/terraform2.tfstate test/tomcat1.nps test/tomcat2.nps我想排除以A和pid257708开头的内容作为附录写在我的回答中。谢谢,它与“if not key['key'].startswith(排除路径):”一起工作。不客气。请接受我的答案(点击顶部附近的复选标记)并向上投票(点击上面的三角形),如果它对你有用的话。这是在这个网站上表达“谢谢”的首选方式。是的,我试过了,但没有得到我想要的结果。它给我带来了这个:A/A/B/DC/DC/LastAWRReport-200607-103252.html DC/LastAWRReport-200607-104438.html DC/LastAWRReport-200607-123731.html DC/hs_err_pid1003103.log DC/hs_err_pid257708.log DC/hs_err_pid367738.log test/test/test/terraform/terraform.tfstate test/terraform2.tfstate test/tomcat1.nps test/tomcat2.nps我想排除以A和pid257708开头的内容作为附录写在我的回答中。谢谢,它与“if not key['key'].startswith(排除路径):”一起工作。不客气。请接受我的答案(点击顶部附近的复选标记)并向上投票(点击上面的三角形),如果它对你有用的话。这是在本网站表达“谢谢”的首选方式。