我试图将CSV导出到Azure,但是当文件系统/容器不存在时,代码会中断。我也读过文档,但是我似乎找不到任何对这种情况有帮助的东西。
如果用户指定的容器不存在,如何在Azure中创建容器?
当前代码:
try:
file_system_client = service_client.get_file_system_client(file_system="testfilesystem")
except Exception:
file_system_client = service_client.create_file_system(file_system="testfilesystem")回溯:
(FilesystemNotFound) The specified filesystem does not exist.
RequestId:XXXX
Time:2021-03-31T13:39:21.8860233Z发布于 2021-04-01 03:27:55
这里不应该使用try catch模式,因为Azure Data lake gen2 library有内置的exists() method for file_system_client。
首先,确保您已经安装了最新版本库:azure-存储-文件-datalake 12.3.0。如果您不确定使用的是哪个版本,请使用pip show azure-storage-file-datalake命令检查当前版本。
然后您可以使用下面的代码:
from azure.storage.filedatalake import DataLakeServiceClient
service_client = DataLakeServiceClient(account_url="{}://{}.dfs.core.windows.net".format(
"https", "xxx"), credential="xxx")
#the get_file_system_client method will not throw error if the file system does not exist, if you're using the latest library 12.3.0
file_system_client = service_client.get_file_system_client("filesystem333")
print("the file system exists: " + str(file_system_client.exists()))
#create the file system if it does not exist
if not file_system_client.exists():
file_system_client.create_file_system()
print("the file system is created.")
#other code我已经在本地测试过,它可以成功地工作:

https://stackoverflow.com/questions/66889092
复制相似问题