Converting argv to variable

T

tgiles

(now that I've posted in the wrong flipping newsgroup the first thing,
here's my question to the lovely python folks)

I've taken a year off (or so) using Python and the first thing I run
into utterly stumped me. Been too long and none of the searches I've
done seems to have helped any.
Basically, I'm trying to create a little script which will make a new
directory, using the argument passed to it to give the directory a
name:

#!/usr/bin/python

import sys, os

newDirectory = str(sys.argv[1:])

currentPath = str(os.getcwd())

create = currentPath + '/' + newDirectory

print create

# os.mkdir(create)

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']

I know there must be some way to convert the argument to a 'normal'
variable, but it escapes me how to do so. Any pointers?

Thanks!

tom
 
T

Tim Chase

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
 
K

Klaus Alexander Seistrup

Tom skrev:
newDirectory = str(sys.argv[1:])

Try

newDir = '/'.join(sys.argv[1:])
or
newDir = sys.argv[1]
or
for newDir in sys.argv[1:]:
:

or something along those lines, depending on how you wish to
interpret the commandline.

Cheers,
 
T

Terry Reedy

tgiles said:
(now that I've posted in the wrong flipping newsgroup the first thing,
here's my question to the lovely python folks)

I've taken a year off (or so) using Python and the first thing I run
into utterly stumped me. Been too long and none of the searches I've
done seems to have helped any.
Basically, I'm trying to create a little script which will make a new
directory, using the argument passed to it to give the directory a
name:

#!/usr/bin/python
import sys, os
newDirectory = str(sys.argv[1:])

If you are only going to make one dir at a time, newDir = sys.argv[1]
currentPath = str(os.getcwd())
create = currentPath + '/' + newDirectory

os.path has platform-independent function to do this

tjr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top