问题
I am trying to get the last modification time of each file present in azure data lake.
files = dbutils.fs.ls('/mnt/blob')
for fi in files: print(fi)
Output:-FileInfo(path='dbfs:/mnt/blob/rule_sheet_recon.xlsx', name='rule_sheet_recon.xlsx', size=10843)
Here i am unable to get the last modification time of the files. Is there any way to get that property.
I tries this below shell command to see the properties,but unable to store it in python object.
%sh ls -ls /dbfs/mnt/blob/
output:- total 0
0 -rw-r--r-- 1 root root 13577 Sep 20 10:50 a.txt
0 -rw-r--r-- 1 root root 10843 Sep 20 10:50 b.txt
回答1:
We don't have direct method to get those details . But We got those details based on the following simple python code .
Example : Consider, you want to get the all subdirectories and files in adls path container_name/container-Second --- You can use below code
from pyspark.sql.functions import col
from azure.storage.blob import BlockBlobService
from datetime import datetime
import os.path
block_blob_service = BlockBlobService(account_name='account-name', account_key='account-key')
container_name ='container-firstname'
second_conatainer_name ='container-Second'
#block_blob_service.create_container(container_name)
generator = block_blob_service.list_blobs(container_name,prefix="Recovery/")
report_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
for blob in generator:
length = BlockBlobService.get_blob_properties(block_blob_service,container_name,blob.name).properties.content_length
last_modified = BlockBlobService.get_blob_properties(block_blob_service,container_name,blob.name).properties.last_modified
file_size = BlockBlobService.get_blob_properties(block_blob_service,container_name,blob.name).properties.content_length
line = container_name+'|'+second_conatainer_name+'|'+blob.name+'|'+ str(file_size) +'|'+str(last_modified)+'|'+str(report_time)
print(line)
来源:https://stackoverflow.com/questions/58237338/how-to-get-the-last-modification-time-of-each-files-present-in-azure-datalake-st