Skip to content
Advertisement

How to check if a folder exist or not in Azure container using Python?

I have a storage container and inside that I have 3 levels of directories as follow:

JavaScript

I need to check are there any blob present in Folder3 or even better just check if Folder3 exist or not. I tried to use
blob_exist = BlobClient.from_connection_string(conn_str = os.getenv("conString"), container_name="Container",blob_name="Folder1/Folder2/Folder3").exists()

It always returns False irrespective of folder exists or not. Can anybody tell me how can I achieve this?
I know empty folders doesn’t exist in Blob Containers, but my intension is to check if a folder exists, then continue other business logic.

Advertisement

Answer

It’s impossible to directly check if a folder exists in blob storage. But you can use the list_blobs() method and the name_starts_with parameter.

For example:

JavaScript
Advertisement