How to check if file exists in Google Cloud Storage?(如何检查文件是否存在于 Google Cloud Storage 中?)
问题描述
我有一个脚本,我想检查存储桶中是否存在文件,如果不存在则创建一个.
我尝试使用 os.path.exists(file_path) where p>file_path = "/gs/testbucket",但是我得到了一个找不到文件的错误.
我知道我可以使用 files.listdir() API 函数列出位于路径中的所有文件,然后检查我想要的文件是否是其中之一.但我想知道是否有另一种方法来检查文件是否存在.
我猜没有函数可以直接检查给定路径的文件是否存在.
我创建了一个函数,该函数使用 files.listdir() API 函数列出存储桶中的所有文件并将其与我们想要的文件名匹配.如果找到则返回 true,否则返回 false.
I have a script where I want to check if a file exists in a bucket and if it doesn't then create one.
I tried using os.path.exists(file_path) where file_path = "/gs/testbucket", but I got a file not found error.
I know that I can use the files.listdir() API function to list all the files located at a path and then check if the file I want is one of them. But I was wondering whether there is another way to check whether the file exists.
I guess there is no function to check directly if the file exists given its path.
I have created a function that uses the files.listdir() API function to list all the files in the bucket and match it against the file name that we want. It returns true if found and false if not.
这篇关于如何检查文件是否存在于 Google Cloud Storage 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检查文件是否存在于 Google Cloud Storage 中?
基础教程推荐
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
