What does the sys module do in python?
and what is sys.argv?

Recommended Answers

All 9 Replies

That's exactly where I checked. I don't understand what the documentation says..
More specifically I don't understand this
"sys.argv¶

The list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string."

Member Avatar for iamthwee
commented: You're a big meanie! +6

Maybe a little example will help ...

import sys

args = sys.argv

print(args)
print( "Source  file: %s" % args[0] )
# use any of these arguments in your code
print( "Argument one: %s" % args[1] )
print( "Argument two: %s" % args[2] )


"""
my output after saving this code as sysargs1.py and 
entering a few arguments on the command line -->
['D:/Python25/Bull/sysargs1.py', 'myarg1', 'myarg2']
Source  file: D:/Python25/Bull/sysargs1.py
Argument one: myarg1
Argument two: myarg2
"""
commented: A short ,clear post. .I deal for forums +1

So for the last two to have values, is it compulsory for the program to be run via command line?

You can run it with module subprocess as well

import subprocess
child = subprocess.Popen("sysargs1.py myarg1 myrag2", shell=True)

What does that module do?

It allows your program to launch other programs and communicate with them.

commented: thanks.. +1

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.