'Deleting a file from Google Cloud using fsspec

I am currently using fsspec in order to read and write files to my google cloud buckets with the code as shown below:

with fs.open(gcs_file_tmp, "rb") as fp:
    gcs_file_content = fp.read()

Now I want to remove a file from a GCS bucket, but I cannot seem to find the code for it. Reading the documentation here, it seems as though there are some rm based functions and also some delete functions but they don't seem to work as they cannot be called as such fs.rm(...) or so



Solution 1:[1]

fs.open() returns fsspec.core.OpenFile instance, having fs property.
When you open GCS file, fs is GCSFileSystem.
When you open local file, fs is LocalFileSystem.
I understand fsspec absorbs difference between file systems, as class AbstractFileSystem.
We can call delete() of it.

In this way, we should pass target file path again(we can use path property if you like).

# for GCS
f = fsspec.open("gs://bucketHoge/fuga.txt")
f.fs.delete(f.path)

# for local
f = fsspec.open("/tmp/fuga.txt")
f.fs.delete(f.path)

I wonder there is a better way...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 toshi