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.
I’ll explain by example.
First, take a look at
. It has three
NYT_STATE_DATA = sys.argv USA_STATE_CODE_DATA = sys.argv OUT_FNAME = sys.argv
sys.argv is the script name.
Take a closer look at
, 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
Let’s say I have this nyt_state_data.py
locally. The location is
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
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
sys are separated by space.
My explanation is based on sys.argv - Command Line Arguments in Python [Part 1] by Sapnesh Naik . ↩︎
Last modified on 2020-11-29