I would advice a library for building CLI applications,over using
Typer is easy to use and really good.
Here a example with your code.
sys.argv
.Typer is easy to use and really good.
Here a example with your code.
# file_rm.py from pathlib import Path import sys import os import typer app = typer.Typer() @app.command() def remove_files(folderpath: str, files: str): '''Remove files based on patrtern given''' os.chdir(folderpath) collection = [] collection = files.split(",") for coll in collection: files = Path.cwd().glob(f"*{coll}*") for file in files: print(file) # Test print before delete #file.unlink(missing_ok=True) if __name__ == "__main__": app()So now will get
help
generated automatic.λ python file_rm.py --help Usage: file_rm.py [OPTIONS] FOLDERPATH FILES Remove files based on patrtern given ┌─ Arguments ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ * folderpath TEXT [default: None] [required] │ │ * files TEXT [default: None] [required] │ └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ ┌─ Options ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ --install-completion [bash|zsh|fish|powershell|pwsh] Install completion for the specified shell. [default: None] │ │ --show-completion [bash|zsh|fish|powershell|pwsh] Show completion for the specified shell, to copy it or customize the installation. [default: None] │ │ --help Show this message and exit. │ └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘Test that his work.
G:\div_code\answer\mb # Now give argument after file λ python file_rm.py C:/test_data ab,xy,py,cd,ef,gh C:\test_data\ab11.tx