remove_by_pattern

remove_by_pattern#

remove_by_pattern(directory: str | CloudPath | Path, name_with_wildcard: str = '*', extension: str | None = None) None[source]#

Remove files corresponding to a pattern from a directory.

Parameters:
  • directory (AnyPathStrType) – Directory where to find the files

  • name_with_wildcard (str) – Filename (wildcards accepted)

  • extension (str) – Extension wanted, optional. With or without point. (yaml or .yaml accepted)

Example

>>> directory = 'D:/path/to/folder'
>>> os.listdir(directory)
["huhu.exe", "blabla.geojson", "haha.txt", "blabla"]
>>>
>>> remove(directory, "blabla*")
>>> os.listdir(directory)
["huhu.exe", "haha.txt"] # Removes also directories
>>>
>>> remove(directory, "*", extension="txt")
>>> os.listdir(directory)
["huhu.exe"]