Search code examples
python-3.xazureazure-filesazure-storage-files

Azure File Share - Recursive Directory Search like os.walk


I am writing a Python script to download files from Azure File Share. The structure of the File Share is as below:

/analytics/Part1/file1.txt
/analytics/Part1/file2.txt
/analytics/mainfile.txt
/analytics/Part1/Part1_1/file11.txt

I tried to use the following lines in my script but it looks for files and directories only at the root directory level.

fileshareclient = ShareClient(
    account_url=args.get('AccountURL'),
    credential=args.get('SASKey'),
    share_name=args.get('FileShare')
)

fileLst = list(
    fileshareclient.list_directories_and_files('analytics')
)

The output is:

/analytics/mainfile.txt  --> File
/analytics/Part1 --> Dir

But, I am looking for something like os.walk() function in Python here to achieve this recursive directory walk. Any idea if such function is available in Azure File Service Python API?


Solution

  • The built-in list_directories_and_files() method of the Azure Storage File Share client library for Python azure-storage-file-share only lists the root directories and files. If you want to something like os.walk(), you should write the method by yourself.

    Here, I write a function which can recursively list all the files / directories and it works fine(please feel free to modify it if it does not meet your need):

    from azure.storage.fileshare import ShareServiceClient
    
    def list_recursive(directory_client,directory_name):
        sub_client = directory_client.get_subdirectory_client(directory_name)
        myfiles = sub_client.list_directories_and_files()
    
        for file in myfiles:
            print(file.get('name'))
            if file.get('is_directory'):
                list_recursive(sub_client,file.get('name'))
    
    
    if __name__ == '__main__':
        conn_str="xxxx"
        file_service = ShareServiceClient.from_connection_string(conn_str)
    
        share_client = file_service.get_share_client("your_share_name")
        d_client = share_client.get_directory_client("your_directory_name")
        myfiles = d_client.list_directories_and_files()
    
        for file in myfiles:
            print(file.get('name'))
            if file.get('is_directory'):
                list_recursive(d_client,file.get('name'))