Table of Contents

Python code to print program name and arguments passed through command line.

 

Python code implementation without user defined functions & classes

Code: 
#Print the program name and arguments passed through command line
from sys import argv
list_of_arguments = []
list_of_arguments = argv

print(list_of_arguments)
Executing Python Script through Command line: 
python program.py I am a Python Program
Output: 
['program.py', 'I', 'am', 'a', 'Python', 'Program']

 

Python code implementation using function

 

Code: 
#Print the program name and arguments passed through command line using function

def list_of_arguments():
    from sys import argv
    list_of_command_line_arguments = []
    list_of_command_line_arguments = argv
    return list_of_command_line_arguments

print(list_of_arguments())

 

Executing Python Script through Command line: 
python program.py I am a Python Program
Output: 
['program.py', 'I', 'am', 'a', 'Python', 'Program']

 

Python code implementation using Classes

Code: 
#Print the program name and arguments passed through command line using Classes

class List_of_arguments(object):
    def list_of_arguments_fun(self):
        from sys import argv
        list_of_command_line_arguments = []
        list_of_command_line_arguments = argv
        return list_of_command_line_arguments


#Create an Object
argument_list = List_of_arguments()

#Call the function using Object
print(argument_list.list_of_arguments_fun())

 

Executing Python Script through Command line: 
python program.py I am a Python Program
Output: 
['program.py', 'I', 'am', 'a', 'Python', 'Program']

 

 

 

Enjoy Python Code By Pythonbaba 🙂