Posts: 119
Threads: 66
Joined: Sep 2022
Sep-26-2022, 11:34 AM
(This post was last modified: Sep-26-2022, 12:14 PM by snippsat.
Edit Reason: Fix code tag
)
Hi Team,
how to accept multiple values or a list of values from command prompt. and store into single variable.
folderpath, next list of file names to delete.
accept "ab,xy,py,cd,ef,gh" and store into single variable.
**python test.py "C:/Users/malle/OneDrive/Desktop/C/test_data" "ab,xy,py,cd,ef,gh"**
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
from pathlib import Path
import sys
def remove_files(folderpath, files):
import os
os.chdir(folderpath)
collection = []
collection = files.split( "," )
for coll in collection:
files = Path.cwd().glob( f "*{coll}*" )
for file in files:
file .unlink(missing_ok = True )
if __name__ = = "__main__" :
pattern = "ab,xy,py,cd,ef,gh"
root = "C:/Users/malle/OneDrive/Desktop/C/test_data"
remove_files(root, pattern)
|
Posts: 34
Threads: 5
Joined: Aug 2022
(Sep-26-2022, 11:34 AM)mg24 Wrote: Hi Team,
how to accept multiple values or a list of values from command prompt. and store into single variable.
folderpath, next list of file names to delete.
accept "ab,xy,py,cd,ef,gh" and store into single variable.
**python test.py "C:/Users/malle/OneDrive/Desktop/C/test_data" "ab,xy,py,cd,ef,gh"**
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
from pathlib import Path
import sys
def remove_files(folderpath, files):
import os
os.chdir(folderpath)
collection = []
collection = files.split( "," )
for coll in collection:
files = Path.cwd().glob( f "*{coll}*" )
for file in files:
file .unlink(missing_ok = True )
if __name__ = = "__main__" :
pattern = "ab,xy,py,cd,ef,gh"
root = "C:/Users/malle/OneDrive/Desktop/C/test_data"
remove_files(root, pattern)
|
You could use sys.argv for that!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import sys
arguments = sys.argv
values = arguments[ 1 :]
print (values)
|
Posts: 7,324
Threads: 123
Joined: Sep 2016
Sep-26-2022, 01:53 PM
(This post was last modified: Sep-26-2022, 01:53 PM by snippsat.)
I would advice a library for building CLI applications,over using sys.argv .
Typer is easy to use and really good.
Here a example with your code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
from pathlib import Path
import sys
import os
import typer
app = typer.Typer()
@app .command()
def remove_files(folderpath: str , files: str ):
os.chdir(folderpath)
collection = []
collection = files.split( "," )
for coll in collection:
files = Path.cwd().glob( f "*{coll}*" )
for file in files:
print ( file )
if __name__ = = "__main__" :
app()
|
So now will get help generated automatic.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
λ 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.
1 2 3 4 |
G:\div_code\answer\mb
λ python file_rm.py C: / test_data ab,xy,py,cd,ef,gh
C:\test_data\ab11.tx
|
carecavoador likes this post
Posts: 119
Threads: 66
Joined: Sep 2022
Hi snippsat,
Superb ! it worked as expected !
is there any other way we can achieve this.
without using global keyboard.
files = Path.cwd().glob(f"*{coll}*") just for learning purpose.
@app.command() what this line mean.
Posts: 6,814
Threads: 20
Joined: Feb 2020
Sep-27-2022, 02:20 PM
(This post was last modified: Sep-27-2022, 02:20 PM by deanhystad.)
Click on the typer link and read about @app.command()
Posts: 7,324
Threads: 123
Joined: Sep 2016
Sep-27-2022, 05:58 PM
(This post was last modified: Sep-27-2022, 05:58 PM by snippsat.)
(Sep-27-2022, 10:03 AM)mg24 Wrote: without using global keyboard.
files = Path.cwd().glob(f"*{coll}*") just for learning purpose. Hmm global keyboard? maybe you meant global keyword.
It not a global keyword and it's ok to use like this.
Path.glob a trick it to make make it recursively is to add ** .
1 |
files = Path.cwd().glob( f "**/{coll}*" )
|
(Sep-27-2022, 10:03 AM)mg24 Wrote: @app.command() what this line mean It's a decorator this mean that can get a clean interface and hide all code that needed in background.
The author of Typer is also author of FastAPI where you see same decorator pattern.
Flask was one the library's first that used this way pattern a lot.
Flask
1 2 3 4 5 6 7 |
from flask import Flask
app = Flask(__name__)
@app .route( "/" )
def hello_world():
return "<p>Hello, World!</p>"
|
FastAPI
1 2 3 4 5 6 7 |
from fastapi import FastAPI
app = FastAPI()
@app .get( "/" )
def read_root():
return { "Hello" : "World" }
|
Typer.
1 2 3 4 5 6 7 |
import typer
app = typer.Typer()
@app .command()
def hello(name: str ):
print ( f "Hello {name}" )
|
|