Skip to main content
 首页 » 编程设计

python之如何从 Python 中删除 GCS 文件夹

2025年05月04日13zfyouxi

使用 https://github.com/googleapis/google-cloud-python/tree/master/storage或 https://github.com/GoogleCloudPlatform/appengine-gcs-client ,我可以通过指定文件名来删除文件,但似乎没有删除文件夹的方法。

有什么方法可以删除文件夹吗?

我在 stackvoerflow 中找到了这个 ( Google Cloud Storage: How to Delete a folder (recursively) in Python ),但这个答案只是删除文件夹中的所有文件,而不是删除文件夹本身。

请您参考如下方法:

您引用的 anwser 中提到的代码有效,前缀应如下所示:

from google.cloud import storage 
 
storage_client = storage.Client() 
bucket = storage_client.get_bucket('my-bucket') 
 
blobs = bucket.list_blobs(prefix='my-folder/') 
 
for blob in blobs: 
    blob.delete()