【发布时间】:2021-10-15 04:09:17
【问题描述】:
我有这段代码,我只想要以没有中间空文件夹的文件结尾的路径。例如:
data/folder1/folder2
data/folder1/folder3/folder4/file1.txt
data/folder5/file2.txt
从那些我只想要的路径:
data/folder1/folder3/folder4/file1.txt
data/folder5/file2.txt
我正在使用此代码,但它也为我提供了以目录结尾的路径:
subfolders = set()
current_path = None
result = await self.s3_client.list_objects(Bucket=bucket, Prefix=prefix)
objects = result.get("Contents")
try:
for obj in objects:
current_path = os.path.dirname(obj["Key"])
if current_path not in subfolders:
subfolders.add(current_path)
except Exception as exc:
print(f"Getting objects with prefix: {prefix} failed")
raise exc
【问题讨论】:
-
相关(用于查找文件夹):stackoverflow.com/questions/35803027/…