简体   繁体   English

使用 Python 从 Azure 容器读取文件的动态文件路径?

[英]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.此文件位于容器中的 Azure 存储上,每当上传文件时都需要自动运行代码。

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.我是 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.如果要确定 Azure 存储上是否存在文件,请使用exists方法。 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 从 Azure Blob 容器读取文件 - How to read a file from Azure Blob Container using Python 在PYTHON中使用SAS URI从AZURE BLOB CONTAINER下载文件 - Download file from AZURE BLOB CONTAINER using SAS URI in PYTHON 使用Python从MS Azure读取和写入文件 - Read and Write file from MS Azure using Python 如何使用 python 和数据框从 csv 文件中读取动态数据 - How to read a dynamic data from csv file using python and dataframe 使用Python读取具有奇怪的Windows路径的文件 - Read file with weird windows path using python 使用 Python 读取 Excel 动态文件参考 - Read Excel dynamic file reference using Python 从 python 中的 azure blob 存储读取文件 - read file from azure blob storage in python 我们如何使用 python 将存储容器中的输入文件提供给 azure 语音 api - How can we give the input file from storage container to azure speech api using python 使用 Jupyter notebook 从存储在 azure 容器中的 zip blob 中提取特定文件 - Extract particular file from zip blob stored in azure container with python using Jupyter notebook 如何使用带有 python 的 Azure 函数写入 blob 容器中的文本文件? - How to write to a text file in a blob container using Azure function with python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM