简体   繁体   中英

Dynamic file path to read a file from Azure Container using Python?

I have to fetch a file and run the code daily based on the input file. This file is on Azure Storage in a container and the code needs to be run automatically whenever a file is uploaded.

The file path is like:

path = f"adls:/{container_name}/{folder}/" + str(year) + "/" + str(month) + "/" + str(day)
file_name = "data_"+str(year)+"_"+str(month)+"_"+str(day)+".csv"

The file name changes daily because of the format. The folder within which this file would be uploaded also changes daily.

This is what I've done so far:

def find_file(current_date, path):
    current_date = datetime.datetime.now()
    year = current_time.year
    month = current_time.month
    day = current_time.day

    path = f"adls:/{container_name}/{folder}/" + str(year) + "/" + str(month) + "/" + str(day)
    file = "data_"+str(year)+"_"+str(month)+"_"+str(day)+".csv"

    if os.path.isfile(path) and os.access(path, os.R_OK):
       print("File exists and is readable")
    else:
       print("Either the file is missing or not readable")
       sys.exit()

    return file

find_file(current_date, path)

I'm checking whether the file exists in the given path. If there's a file with the given name, the code should run and read the file, if not the code should abort.

The above code obviously isn't correct. I'm new to functions in Python. Can someone correct me where am I going wrong?

If you want to determine whether a file exists on Azure Storage, make use of the exists method. You could refer to here .

from azure.storage.blob import BlobServiceClient

blob_service_client = BlobServiceClient.from_connection_string("connection_string")
blob_client = blob_service_client.get_blob_client(container="container_name", blob="my_blob")

isExist = blob_client.exists()
print(isExist)

if isExist:
    # download blob file
    stream = blob_client.download_blob()
    print("File exists and is readable")
else:
    print("Either the file is missing or not readable")

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