python-3.x - 通过 python 检查 azure data Lake Storage gen2 中是否存在文件
问题描述
嗨,我想知道 ADLS Gen 2 中的文件存在
file_client = service_client.get_file_client(file_system='filelayer', file_path='my_file.txt')
这给出了file_client,但是如何检查文件是否存在,因为我试图重命名它并给出错误(如果不存在):
(SourcePathNotFound) The source path for a rename operation does not exist
所以我只想file_client.rename_file(target_name)
在文件存在时才进行重命名。
解决方案
没有 file_exists,但您可以使用 get_file_properties()
def exists(file_client: DataLakeFileClient):
try:
file_client.get_file_properties()
return True
except ResourceNotFoundError:
return False
file_client = service_client.get_file_client(file_system='filelayer', file_path='my_file.txt')
if exists(file_client):
file_client.rename_file(targetName)
但是,有些人(包括我)考虑了直接通过 try/catch 来解决问题的 Pythonic 方法:
file_client = service_client.get_file_client(file_system='filelayer', file_path='my_file.txt')
try:
file_client.rename(target_name)
except ResourceNotFoundError:
# log that the files does not exist or not, up to you
pass
``
推荐阅读
- python - ValueError:检查目标时出错:预期 activation_7 的形状为 (154, 154, 1) 但得到的数组的形状为 (200, 200, 3)
- xml - 如何在 perl 中使用 XML::Parser 写回 XML?
- amazon-redshift - 如何在一个日期范围内复制 name_ids
- php - 将我的帖子从 single.php 导航到自定义模板
- karate - 空手道的语言特定扩展
- angular - 当页面重新加载时,ngrx 存储数据被刷新到初始状态
- ios - Mixpanel 推送通知不适用于 iOS
- javascript - ng-audio 在带有时间轨迹的 chrome 上无法正常工作
- php - 有人可以将来自不同主机的表单数据发送到我的 PHP 脚本,该脚本将获取的数据插入 MySQL 吗?如果是这样,我们如何保护它?
- c# - 使用 ActionFilter 查看假设的 ActionArgument