Tanvir Shaikh Tanvir Shaikh -4 years ago 214
Python Question

Python GAE - How to check if file exists in Google cloud storage

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 get 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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download