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

1 answer

  • answered 2022-04-23 23:44 toshi

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

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum