What is sys.argv in Python and How to Use It

Hongtao Hao / 2020-10-05

While I was updating the data for Professor YY ‘s COVID19-data project , I noticed some lines of codes containing sys.argv. I didn’t understand it in the begining, nor was I sure how to use it. I understood it later. This post is to explain what sys.argv is and how to use it.

Python sys.argv is essentially a variable from the sys module. This variable contains arguments that are to be passed to the python script you are calling in the command line1.

I’ll explain by example.

First, take a look at nyt_state_data.py . It has three sys.argv variables:

NYT_STATE_DATA = sys.argv[1]
USA_STATE_CODE_DATA = sys.argv[2]
OUT_FNAME = sys.argv[3]

By default, sys.argv[0] is the script name.

Take a closer look at nyt_state_data.py , and you’ll find that the first two sys.argv variables are CSV files to be read by pandas and the last one is a string or file location for the pandas.DataFrame.to_csv function.

Let’s say I have this nyt_state_data.py locally. The location is Desktop/Covid19-data/scripts/nyt_state_data.py.

In terminal, I’ll call this script first, followed by three arguments passed to this script:

python Desktop/covid19-data/scripts/nyt_state_data.py https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv https://raw.githubusercontent.com/hongtaoh/covid19-data/master/data_sources/wikipedia/ISO3166/usa_state_code.csv Desktop/covid19-data/output/cases/cases_us_states_nyt_UP_TO_$(date -v -1d '+%d_%B_%Y').csv

The date setting is based on These two sources: 1 , 2

I know above code is very messay since the URLs of the two CSV files are pretty long. I’ll demystify it this way:

python YourScript.py Arg1 Arg2 Arg3

Arguments for sys are separated by space.

  1. My explanation is based on sys.argv - Command Line Arguments in Python [Part 1] by Sapnesh Naik . ↩︎

Last modified on 2020-10-06