简体   繁体   中英

How do you delete a file from an Azure Data Lake using the Python SDK?

I'm using the azure-storage-file-datalake plugin for Python 3.8. The SDK is described in great depth here -- https://docs.microsoft.com/en-us/python/api/azure-storage-file-datalake/azure.storage.filedatalake.datalakedirectoryclient?view=azure-python , but no where is there a description of whether you can delete a file from the data lake, the SDK only describes deleting directories. Is it possible to delete a file?

In response to the answer given, I tried this...为了回应给出的答案,我尝试了这个......

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

but the last line results in this error

TypeError('element indices must be integers')

Update:

First, I'm installing the latest python sdk for adls gen2. Using the command below:

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

Here is my test code:

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**")

And the test result:

在此处输入图像描述


Original answer:

If you want to delete a file, you should take a look at DataLakeFileClient class . In this class, it has a delete_file method. Please take a look at this article for its usage.

Or when you use DataLakeDirectoryClient class , you can get the file client by using get_file_client() method of DataLakeDirectoryClient instance, then call the delete_file() method.

Please let me know if you still have more issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM