473,394 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Converting argv to variable

(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
3 1277
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Bushido Hacks | last post by:
Hey c.l.c++ and/or c.g.a.opengl posters, How do I convert a hexidecimal string, traditionally used for defining colors with HTML, into a floating point array? In other words, how do I convert...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
32
by: mnaydin | last post by:
Assume the main function is defined with int main(int argc, char *argv) { /*...*/ } So, is it permitted to modify the argv array? The standard says "The parameters argc and argv and the strings...
2
by: tgiles | last post by:
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...
3
by: Howler | last post by:
Hello all, I am having a hard time seeing what I am doing wrong with a program I am having to write that converts pbm monochrome images into a similar pgm file. The problem I am having is...
1
by: jamesd | last post by:
First off my programming experience is very limited and I haven't used C/C++ in the past 4/5 years so I'm fairly c**p at it. Basically I'm trying to write a function that opens a .wav file and...
22
by: kotlakirankumar | last post by:
please help me out the program for converting the integers to roman numerals using files in the c language from 1-5000 range send the program to my mail id ::::::: kotlakirankumar@gmail.com...
5
by: maker.rain1 | last post by:
Hello All, I have come across a problem as explained below in a sample. Please help me if anyone has any ideas to solve this. I have a #define as defined below. #define MAX 200 int...
35
by: Sean Farrow | last post by:
Hi: What is best and safest way of converting a char* to a const char *? Can I use const_cast? Cheers Sean.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.