[英]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.