Python Forum
accept command line argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
accept command line argument
#2
(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"**

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!

import sys

"""
sys.argv returns a list object containing all the arguments passed when
running your script, including the script itself.

In this case, running the command python test.py "ab,xy,py,cd,ef,gh"
will return: ['test.py', 'ab,xy,py,cd,ef,gh']

If, instead of the list of values enclosed in double quotes you pass
the individual values separated by spaces, you will get a list with
the script name plus every value you passed.

python test.py ab xy py cd ef gh
will return: ['test.py', 'ab', 'xy', 'py', 'cd', 'ef', 'gh']

Than you can get only the values you want using list comprehension:
values = sys.argv[1:]

The above gets all the values from sys.argv except the first index,
which is the script file. 
"""

arguments = sys.argv
values = arguments[1:]

print(values)
Reply


Messages In This Thread
accept command line argument - by mg24 - Sep-26-2022, 11:34 AM
RE: accept command line argument - by carecavoador - Sep-26-2022, 12:40 PM
RE: accept command line argument - by snippsat - Sep-26-2022, 01:53 PM
RE: accept command line argument - by mg24 - Sep-27-2022, 10:03 AM
RE: accept command line argument - by deanhystad - Sep-27-2022, 02:20 PM
RE: accept command line argument - by snippsat - Sep-27-2022, 05:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  501 Server cannot accept argument anna17 0 230 Apr-11-2024, 01:08 AM
Last Post: anna17
  How to accept facebook cookies using python selenium? pablo86ad 0 247 Apr-06-2024, 09:19 PM
Last Post: pablo86ad
  Command line argument issue space issue mg24 5 1,366 Oct-26-2022, 11:05 PM
Last Post: Yoriz
  Accessing varying command line arguements Rakshan 3 2,081 Jul-28-2021, 03:18 PM
Last Post: snippsat
  How to input & output parameters from command line argument shantanu97 1 2,606 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Passing List of Objects in Command Line Python usman 7 3,247 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  I need my compiled Python Mac app to accept a file as a parameter Oethen 2 2,460 May-10-2020, 05:57 PM
Last Post: Oethen
  Taking Multiple Command Line Argument Input bwdu 6 4,095 Mar-29-2020, 05:52 PM
Last Post: buran
  SyntaxError: positional argument follows keyword argument syd_jat 3 5,883 Mar-03-2020, 08:34 AM
Last Post: buran
  python 3 from command line Dixon 1 2,039 Mar-01-2020, 08:35 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020