The Simplest Introduction To Sys.Argv As A Way To Pass Arguments From Command Line

  1. at the bottom of your file, use the pattern to run script immediatelly if called from a shell (if __name__ == "__main__":...)
  2. use argv[<n>] to refer to the first, second, and nth positional argument
  3. the number must start from 1 not from 0 for example:
# md2med.pyimport sys
def main(doc_name, file_to_publish):
# CODE ACCEPTING THE PARAMS HERE
#...

if __name__ == "__main__":
main(doc_name=sys.argv[1], file_to_publish=sys.argv[2])
>>> python md2med.py test-doc ./test-doc.md

1. ISSUE WITH INTGERS

  • it is required to pass strings
  • TypeError is thrown when working with passed number as an int
>>> python .\pv.py "com-snplow-sales-aws-prod1.collector.snplow.net" 5
TypeError: 'str' object cannot be interpreted as an integer

2. WHAT DOES ARGV MEAN ?

  • in C, there is also argc, which is argument count

3. SOURCES

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Pavol Kutaj

Infrastructure Support Engineer/Technical Writer (Snowplow Analytics) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com