• 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 / System / How to use sys.argv in Python

How to use sys.argv in Python

Author: PFB Staff Writer
Last Updated: August 25, 2020

What is sys.argv?

sys.argv is a list in Python, which contains the command-line arguments passed to the script.

With the len(sys.argv) function you can count the number of arguments.

If you are gonna work with command line arguments, you probably want to
use sys.argv.

To use sys.argv, you will first have to import the sys module.

Example

To show how this works.

(Remember that sys.argv[0] is the name of the script.

import sys
print "This is the name of the script: ", sys.argv[0]
print "Number of arguments: ", len(sys.argv)
print "The arguments are: " , str(sys.argv)

Output

This is the name of the script: sysargv.py
Number of arguments in: 1
The arguments are: [‘sysargv.py’]

If I run it again with additional arguments, I will get this output:

This is the name of the script: sysargv.py
Number of arguments in: 3
The arguments are: [‘sysargv.py’, ‘arg1’, ‘arg2’]

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: System, System & OS Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments 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 Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS Uncategorized 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 Comprehensions in Python
  • How to use sys.argv in Python
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Convert String to List in Python
  • Convert String to Set in Python
  • Largest Element in a List in Python
  • Insert New Column Into a Dataframe in Python
  • List of Dictionaries to Dataframe in Python

Copyright © 2012–2022 · PythonForBeginners.com

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