Converting argv to variable

Tim Chase python.list at tim.thechases.com
Sat Jul 22 09:14:41 EDT 2006


 > newDirectory = str(sys.argv[1:])
[cut]
 > Now, in a perfect universe I would get an output something
 > like the following (if I run the script with the argument
 > 'python':
 >
 > /Volumes/Home/myuser/python
 >
 > However, Python still hangs on to all the fluff and prints
 > out something else:
 >
 > /Volumes/Home/myuser/['python']

Your "newDirectory = ..." line is asking for a slice of a
list, which returns a list.  Python then dutifully converts
that list to a string representation and tacks that onto
your string/path.

What you want is sys.argv[1] (the first argument) not
sys.argv[1:] (a list of all but the first argv--namely, all
the arguments as [0] is the name of your python script)

-tkc








More information about the Python-list mailing list