【问题标题】:Read and write file from Azure Data Lake Storage Gen2 in python在 python 中从 Azure Data Lake Storage Gen2 读取和写入文件
【发布时间】:2021-10-02 07:21:44
【问题描述】:

按照 Microsoft 文档:

使用帐户密钥连接到 Azure Data Lake Storage Gen2:

def initialize_storage_account(storage_account_name, storage_account_key):
    
    try:  
        global service_client

        service_client = DataLakeServiceClient(account_url="{}://{}.dfs.core.windows.net".format(
            "https", storage_account_name), credential=storage_account_key)
    
    except Exception as e:
        print(e)

将文件上传到目录:

def upload_file_to_directory():
    try:
        file_system_client = service_client.get_file_system_client(file_system="my-file-system")

        directory_client = file_system_client.get_directory_client("my-directory/filter")
        
        file_client = directory_client.create_file("my_csv_write.csv")
        local_file = open("C:\\Users\\my_csv_read.csv",'r')

        file_contents = local_file.read()

        file_client.append_data(data=file_contents, offset=0, length=len(file_contents))

        file_client.flush_data(len(file_contents))
        print("File uploaded")

    except Exception as e:
      print(e)

我可以使用此功能将文件从本地上传到 Azure 存储,并且可以正常工作。

但我想做的是从天蓝色存储中读取文件并在天蓝色存储中写入。 我做了什么

def read_and_write_to_directory():
    try:
        file_system_client = service_client.get_file_system_client(file_system="my-file-system")

        directory_client_read = file_system_client.get_directory_client("my-directory")
        directory_client_write = file_system_client.get_directory_client("my-directory/filter")

        file_client_read = directory_client_read.get_file_client("my_csv_read.csv")
        file_path = open(file_client_read,'r')
        file_contents = file_path.read()
        

        file_client_write = directory_client_write.create_file("my_csv_write.csv")
        file_client_write.append_data(file_contents, overwrite=True)
        
    
    except Exception as e:
        print(e)

但它不起作用,

错误

expected str, bytes or os.PathLike object, not DataLakeFileClient

那么从 azure Lake Storage 读取文件并在 azure Lake Storage 中写入的正确方法是什么?

【问题讨论】:

    标签: python azure azure-data-lake-gen2


    【解决方案1】:

    此错误似乎与无法找到目录/文件的路径有关。

    您可以通过调用FileSystemClient.get_paths方法列出目录内容,然后枚举结果。

    此示例打印位于名为 my-directory 的目录中的每个子目录和文件的路径

    def list_directory_contents():
        try:
            
            file_system_client = service_client.get_file_system_client(file_system="my-file-system")
    
            paths = file_system_client.get_paths(path="my-directory")
    
            for path in paths:
                print(path.name + '\n')
    
        except Exception as e:
         print(e)
    

    您可以参考Use Python to manage directories and files in Azure Data Lake Storage Gen2Add a getPathClient() method in DataLakeFileSystemClient 的功能请求

    【讨论】:

      猜你喜欢
      • 2019-09-10
      • 2020-01-13
      • 2021-11-10
      • 1970-01-01
      • 2020-03-09
      • 2021-12-23
      • 2019-11-13
      • 1970-01-01
      • 2020-12-01
      相关资源
      最近更新 更多