How to use glob() to find files recursively?

前端 未结 28 1846
天涯浪人
天涯浪人 2020-11-21 22:54

This is what I have:

glob(os.path.join(\'src\',\'*.c\'))

but I want to search the subfolders of src. Something like this would work:

<
28条回答
  •  故里飘歌
    2020-11-21 23:18

    If the files are on a remote file system or inside an archive, you can use an implementation of the fsspec AbstractFileSystem class. For example, to list all the files in a zipfile:

    from fsspec.implementations.zip import ZipFileSystem
    fs = ZipFileSystem("/tmp/test.zip")
    fs.glob("/**")  # equivalent: fs.find("/")
    

    or to list all the files in a publicly available S3 bucket:

    from s3fs import S3FileSystem
    fs_s3 = S3FileSystem(anon=True)
    fs_s3.glob("noaa-goes16/ABI-L1b-RadF/2020/045/**")  # or use fs_s3.find
    

    you can also use it for a local filesystem, which may be interesting if your implementation should be filesystem-agnostic:

    from fsspec.implementations.local import LocalFileSystem
    fs = LocalFileSystem()
    fs.glob("/tmp/test/**")
    

    Other implementations include Google Cloud, Github, SFTP/SSH, Dropbox, and Azure. For details, see the fsspec API documentation.

提交回复
热议问题