Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I'm currently teaching myself Python and was just wondering (In reference to my example below) in simplified terms what the sys.argv[1] represents. Is it simply asking for input?

#!/usr/bin/python3.1 

# import modules used here -- sys is a very standard one 

import sys 

# Gather our code in a main() function 

def main(): 

print ('Hello there', sys.argv[1]) 

# Command line args are in sys.argv[1], sys.argv[2] .. 

# sys.argv[0] is the script name itself and can be ignored 

# Standard boilerplate to call the main() function to begin 

# the program. 

if __name__ == '__main__': 

main()

1 Answer

0 votes
by (106k points)

The meaning of sys.argv[1] contains the first command-line argument passed to your script.

For example, if your script is named hello.py and your issue:

$ python3.1 hello.py foo

or:

$ chmod +x hello.py

$ ./hello.py foo

Your script will print:

Hello there foo

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
0 votes
2 answers
asked Sep 17, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Jul 13, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Sep 13, 2019 in Machine Learning by shani (120 points)

Browse Categories

...