• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Argv / Command Line Arguments in Python

Command Line Arguments in Python

Author: PFB Staff Writer
Last Updated: June 11, 2023

The sys module in Python provides us with many tools to interact with the system using Python programs. In this article, we will discuss how we can use command line arguments using sys.argv list in Python.

Table of Contents
  1. What is sys.argv List in Python?
  2. How to Use Command Line Arguments in Python?
  3. Conclusion

What is sys.argv List in Python?

In a Python program, sys.argv is the list of command line arguments passed to the program while executing it through the command line interface. Here,

  • sys is the Python sys module.
  • argv represents all the arguments that come along via the command line input.

Thus, sys.argv is basically an array holding the command line arguments of our program.

The first element in the sys.argv list is the name of the program itself. The arguments passed to the program are present at index 1 onwards in the sys.argv list in the same that are passed while executing the program. Don’t forget that the counting starts at zero (0), not one (1).

How to Use Command Line Arguments in Python?

To use command line arguments in Python, you will first have to import the sys module. After this, you can access all the command line arguments using the sys.argv list. To understand how the command line arguments, let us write a program and execute it using command line interface.

import sys
print("Hi, I am PFB")
print("The sys.argv list is:",sys.argv)
sys_argv_length=len(sys.argv)
number_of_arguments=sys_argv_length-1
print("Total command line arguments are:",number_of_arguments)

Output:

Command line arguments in Python Example 1
Command line arguments in Python Example 1

In the above example, we first print the sys.argv list using the print() function. Then, calculated the length of the sys.argv list using the len() function. You can observe that the only element in the sys.argv list is argvsys.py that is the name of the program file.

Now, let us give some command line arguments as input to the program.

import sys
print("Hi, I am PFB")
print("The sys.argv list is:",sys.argv)
sys_argv_length=len(sys.argv)
number_of_arguments=sys_argv_length-1
print("Total command line arguments are:",number_of_arguments)
file_name=sys.argv[0]
first_argument=sys.argv[1]
second_argument=sys.argv[2]
third_argument=sys.argv[3]
fourth_argument=sys.argv[4]
print("The filename is:",file_name)
print("The first command line argument is:",first_argument)
print("The second command line argument is:",second_argument)
print("The third command line argument is:",third_argument)
print("The fourth command line argument is:",fourth_argument)

Output:

Command line arguments in Python Example 2
Command line arguments Example 2

In this example, we passed four command line arguments to the program. You can observe that the first argument, sys.argv[0], is the name of the program as it was invoked,
and sys.argv[1] is the first argument you pass to the program. The next elements in the list are also consecutive command line arguments.

To access only the list of arguments and avoid the file name, you can slice the sya.argv list to access the actual command line arguments using the following syntax.

arguments = sys.argv[1:]

The above statement performs list slicing to select elements from index 1 to the last element of the sys.argv list. After obtaining the list of command line arguments, you can use them in your program however you want.

Conclusion

In this article, we discussed how command line arguments are stored in a python program. We also discussed how to use the command line arguments in our code by slicing the sys.argv list. To learn more about python programming, you can read this article on how to split a text file in Python. You might also like this article on string manipulation in Python.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Argv Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us