0

我正在为 Python 3.8 使用 azure-storage-file-datalake 插件。SDK 在这里进行了深入的描述 - https://docs.microsoft.com/en-us/python/api/azure-storage-file-datalake/azure.storage.filedatalake.datalakedirectoryclient?view=azure-python,但是在哪里没有关于是否可以从数据湖中删除文件的描述,SDK 只描述了删除目录。是否可以删除文件?

编辑:为了回应给出的答案,我尝试了这个......

file = DataLakeFileClient.from_connection_string(
                my_connection_string, 
                file_system_name=filesystem, 
                file_path=path
            )
            file.delete_file()

但最后一行导致此错误

TypeError('element indices must be integers')
4

1 回答 1

1

更新:

首先,我正在为 adls gen2 安装最新的 python sdk。使用以下命令:

pip install azure-storage-file-datalake==12.1.1

这是我的测试代码:

from azure.storage.filedatalake import DataLakeFileClient

conn_str="xxxx"
filesystem="aaa"
file_path="foo2.txt" #if the file is in a directory, like in directory test1, you should specify the path as "test1/foo2.txt"

fileClient = DataLakeFileClient.from_connection_string(
    conn_str=conn_str,
    file_system_name=filesystem,
    file_path=file_path
)

fileClient.delete_file()

print("**completed**")

和测试结果:

在此处输入图像描述


原答案:

如果你想删除一个文件,你应该看看DataLakeFileClient 类。在这个类中,它有一个delete_file方法。请查看这篇文章以了解其用法。

或者当你使用DataLakeDirectoryClient 类get_file_client()时,你可以通过实例的方法获取文件客户端DataLakeDirectoryClient,然后调用该delete_file()方法。

如果您还有更多问题,请告诉我。

于 2020-08-19T01:13:04.470 回答