This is my script
def main(argv):
if len(sys.argv)>1:
for x in sys.argv:
build(x)
if __name__ == "__main__":
main(sys.argv)
so from the command line I write python myscript.py commandlineargument
I want it to skip myscript.py
and simply run commandlineargument
through commandlineargument(n)
so I understand that my for loop doesn't account for this, but how do I make it do that?
Since sys.argv is a list, you can use slicing sys.argv[1:]
:
def main(argv):
for x in argv[1:]:
build(x)
if __name__ == "__main__":
main(sys.argv)
But, if you can only have one script parameter, just get it by index: sys.argv[1]
. But, you should check if the length of sys.argv
is more than 1 and throw an error if it doesn't, for example:
def main(argv):
if len(argv) == 1:
print "Not enough arguments"
return
else:
build(argv[1])
if __name__ == "__main__":
main(sys.argv)