Skip to main content
added 210 characters in body
Source Link
user51621
user51621

Your code can be simplified through the usage of os.walk() and pathlib.Path.

from os import walk
from pathlib import Path


def cleanup(directory: Path, suffixes: set[str]) -> None:
    """Remove unwanted files with the given
    suffixes as well as empty directories.
    """
    
    for root, dirs, files in walk(directory):
        root = Path(root)

        for file in map(lambda filename: root / filename, files):
            if file.suffix in suffixes:
                file.unlink()

        if root.is_dir() and not dirs and not files:
            root.rmdir()


def main() -> None:

    cleanup(Path('folder'), {'.html', '.srt'})


if __name__ == '__main__':
    main()

This way it is more naturally readable, what the code does plus you have directory recursion. By using a separate function (cleanup()), you can also re-use the code with other directories or suffix sets.

Before

$ tree folder/
folder/
├── bar
│   ├── file01.jpg
│   ├── file02.html
│   └── file03.srt
├── foo
│   ├── file01.jpg
│   ├── file02.html
│   └── file03.srt
└── spamm

After

$ tree folder/
folder/
├── bar
│   └── file01.jpg
└── foo
    └── file01.jpg

2 directories, 2 files

Your code can be simplified through the usage of os.walk() and pathlib.Path.

from os import walk
from pathlib import Path


def cleanup(directory: Path, suffixes: set[str]) -> None:
    """Remove unwanted files with the given
    suffixes as well as empty directories.
    """
    
    for root, dirs, files in walk(directory):
        root = Path(root)

        for file in map(lambda filename: root / filename, files):
            if file.suffix in suffixes:
                file.unlink()

        if root.is_dir() and not dirs and not files:
            root.rmdir()


def main() -> None:

    cleanup(Path('folder'), {'.html', '.srt'})


if __name__ == '__main__':
    main()

Before

$ tree folder/
folder/
├── bar
│   ├── file01.jpg
│   ├── file02.html
│   └── file03.srt
├── foo
│   ├── file01.jpg
│   ├── file02.html
│   └── file03.srt
└── spamm

After

$ tree folder/
folder/
├── bar
│   └── file01.jpg
└── foo
    └── file01.jpg

2 directories, 2 files

Your code can be simplified through the usage of os.walk() and pathlib.Path.

from os import walk
from pathlib import Path


def cleanup(directory: Path, suffixes: set[str]) -> None:
    """Remove unwanted files with the given
    suffixes as well as empty directories.
    """
    
    for root, dirs, files in walk(directory):
        root = Path(root)

        for file in map(lambda filename: root / filename, files):
            if file.suffix in suffixes:
                file.unlink()

        if root.is_dir() and not dirs and not files:
            root.rmdir()


def main() -> None:

    cleanup(Path('folder'), {'.html', '.srt'})


if __name__ == '__main__':
    main()

This way it is more naturally readable, what the code does plus you have directory recursion. By using a separate function (cleanup()), you can also re-use the code with other directories or suffix sets.

Before

$ tree folder/
folder/
├── bar
│   ├── file01.jpg
│   ├── file02.html
│   └── file03.srt
├── foo
│   ├── file01.jpg
│   ├── file02.html
│   └── file03.srt
└── spamm

After

$ tree folder/
folder/
├── bar
│   └── file01.jpg
└── foo
    └── file01.jpg

2 directories, 2 files
Source Link
user51621
user51621

Your code can be simplified through the usage of os.walk() and pathlib.Path.

from os import walk
from pathlib import Path


def cleanup(directory: Path, suffixes: set[str]) -> None:
    """Remove unwanted files with the given
    suffixes as well as empty directories.
    """
    
    for root, dirs, files in walk(directory):
        root = Path(root)

        for file in map(lambda filename: root / filename, files):
            if file.suffix in suffixes:
                file.unlink()

        if root.is_dir() and not dirs and not files:
            root.rmdir()


def main() -> None:

    cleanup(Path('folder'), {'.html', '.srt'})


if __name__ == '__main__':
    main()

Before

$ tree folder/
folder/
├── bar
│   ├── file01.jpg
│   ├── file02.html
│   └── file03.srt
├── foo
│   ├── file01.jpg
│   ├── file02.html
│   └── file03.srt
└── spamm

After

$ tree folder/
folder/
├── bar
│   └── file01.jpg
└── foo
    └── file01.jpg

2 directories, 2 files