By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,548 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

Converting argv to variable

P: n/a
(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

Jul 22 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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,

--
Klaus Alexander Seistrup
Copenhagen, Denmark
http://surdej.dk/
Jul 22 '06 #2

P: n/a
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

Jul 22 '06 #3

P: n/a

"tgiles" <tg****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
(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

Jul 22 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.