I’m talking about stuff like this: [file.unlink() for file in files] instead of the more verbose but maybe easier to grasp for python noobs:

for file in files:
    file.unlink()

Maybe with a bit more context:

def _cleanup(self) -> None:                                                                                                                                                                                                             
    dirs, files = partition(lambda f: f.is_file(), self._tmp_dir.rglob("*"))                                                                                                                                                            
    [file.unlink() for file in files]                                                                                                                                                                                                   
    [dir.rmdir() for dir in dirs]                                                                                                                                                                                                       
    self._tmp_dir.rmdir()
  • slurp
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

    Though you might want to consider whether you want it to remove as much as possible in the case of permissions errors, in which case you might want exception handling around each unlink call, with it raising an exception after it’s tried clearing everything if anything went wrong.