Making dir's

Y

yawgmoth7

Hello, I am writing a script that will organize all the code in the
given directory. Well, currently I have it make dir's with something
like:

os.mkdir("C")
os.mkdir("Python")
os.mkdir("ASM")

And so on. That is not very practical, and I wish to change it. I was
wondering if there were any other methods to which I could do this, I
was thinking maybe I could put the dir names in a dictionary then have
something like:
os.mkdir(thedictname)

This doesn't sound like it would work, maybe with a bit of tweaking to
my idea. So, anyone that has any idea's on how I could do this...that
would be great...

Thanks for your help.
 
W

wrongbad

yawgmoth7 said:
And so on. That is not very practical, and I wish to change it. I was
wondering if there were any other methods to which I could do this, I
was thinking maybe I could put the dir names in a dictionary then have
something like:
os.mkdir(thedictname)

Why not use a loop?

dirs = ['ASM', 'C', 'Python']

for dir in dirs:
os.mkdir(dir)
 
J

James Stroud

yawgmoth7 said:
Hello, I am writing a script that will organize all the code in the
given directory. Well, currently I have it make dir's with something
like:

os.mkdir("C")
os.mkdir("Python")
os.mkdir("ASM")

And so on. That is not very practical, and I wish to change it. I was
wondering if there were any other methods to which I could do this, I
was thinking maybe I could put the dir names in a dictionary then have
something like:
os.mkdir(thedictname)

This doesn't sound like it would work, maybe with a bit of tweaking to
my idea. So, anyone that has any idea's on how I could do this...that
would be great...

Thanks for your help.

Assuming something in the standard library doesn't do this already
(shutils, os, os.path)

import os

def mkdirs(pths):
for pth in args:
os.mkdir(pth)

mkdirs(["C","Python, "ASM"])

Is that what you mean?
 
J

James Stroud

yawgmoth7 said:
Hello, I am writing a script that will organize all the code in the
given directory. Well, currently I have it make dir's with something
like:

os.mkdir("C")
os.mkdir("Python")
os.mkdir("ASM")

And so on. That is not very practical, and I wish to change it. I was
wondering if there were any other methods to which I could do this, I
was thinking maybe I could put the dir names in a dictionary then have
something like:
os.mkdir(thedictname)

This doesn't sound like it would work, maybe with a bit of tweaking to
my idea. So, anyone that has any idea's on how I could do this...that
would be great...

Thanks for your help.

Assuming something in the standard library doesn't do this already
(shutils, os, os.path)

import os

def mkdirs(pths):
for pth in pths:
os.mkdir(pth)

mkdirs(["C","Python, "ASM"])

Is that what you mean?
 
C

Christoph Zwerschke

yawgmoth7 said:
os.mkdir("C")
os.mkdir("Python")
os.mkdir("ASM")

And so on. That is not very practical, and I wish to change it. I was
wondering if there were any other methods to which I could do this, I
was thinking maybe I could put the dir names in a dictionary then have
something like:
os.mkdir(thedictname)

For what do you need a dictionary? A list or a set should suffice:

[os.mkdir(d) for d in "C Python ASM".split()]

If some of these directories could already exist, this may be safer:

for d in "C Python ASM".split():
try:
os.mkdir(d)
except OSError:
pass

Another possibility is the following:

os.system("mkdir C Python ASM")

-- Chris
 
K

Klaus Alexander Seistrup

Or os.makedirs():

#v+
makedirs(path [, mode=0777])

Super-mkdir; create a leaf directory and all intermediate ones.
Works like mkdir, except that any intermediate path segment (not
just the rightmost) will be created if it does not exist. This is
recursive.

#v-

Cheers,
 
I

I V

Klaus said:
Or os.makedirs():

Although that may or may not do what the OP wants. I was assuming she
was trying to produce a directory structure like:

/dir/C/
/dir/ASM/
/dir/Python/

os.makedirs() produces a directory hierarchy, like:

/dir/C/ASM/Python/
 

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
474,281
Messages
2,571,397
Members
48,096
Latest member
charlessmith

Latest Threads

Top