Get a reversed list of command line arguments from sys.argv

Posted in python

You can’t loop through sys.argv or args with the reverse() function applied directly to it, but you can append all the sys.argv elements to a list, reverse the list, and then loop through it.

#!/bin/env python
import sys,re

args = []
for arg in sys.argv:
    args.append(arg)

args.reverse()
for arg in args:
    print arg

This is useful if you need to look at the last arguments before deciding what to do, possibly for interoperability with some older code where the order of command line arguments is already set in stone.

If you want to eliminate the program name from the list of arguments (sys.argv[0]), then this will work:

for arg in sys.argv[1:]:
    args.append(arg)
Posted by admica   @   12 October 2009

Related Posts

Like this post? Share it!

Digg Twitter StumbleUpon Delicious Technorati Facebook RSS

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

Previous Post
« Python script to cut some lines from the top a file
Next Post
If netbooks start using ARM chips, how will Microsoft survive? »
Powered by Wordpress   |   Lunated designed by ZenVerse