create a directory in python
July 18th, 2006 by Lawrence David
assuming you’re running some *nix (including mac os x), here’s how to create a directory from within a python script. note that we need to check if the directory exists before creating a directory (if you try and overwrite a directory, python will throw an error).
here goes:
import os;
…
dirname = “my_dir_name”
if not os.path.isdir(“./” + dirname + “/”):
os.mkdir(“./” + dirname + “/”)
note that the last line needs to be indented (silly blogger won’t let me indent that line!)

Hey, I randomly found this page on google the other day when I was working on some python stuff. This post helped. Thanks.
But what i actually wanted to comment on was the header at the top of your theme. I think it’s fantastic. Way to go.
Cheers
it’s what happens when you’ve got too much time on your hands during 1st year of grad school
Super!!!
[...] needed to learn how to create directories using Python and found this great resource (digression: check out this author’s page headers – way [...]
Thanks a bunch, this is exactly what I was looking for.
Thanks!
Nearly 18 months on I have to echo the first commenter’s thoughts – totally cool header.
This also works on Windows unchanged, but like it better like this:
[code]
dirname = "my_dir_name"
if not os.path.isdir(os.path.join(os.getcwd(), dirname)):
os.mkdir(os.path.join(os.getcwd(), dirname))
[/code]
My $0.02. Nice site BTW.
Thanks exactly what I was looking for. Modified Matthew’s code slightly to incorporate recursive directory creation.
#code
import os;
dirname = “mydir”
if not os.path.isdir(os.path.join(os.getcwd(), dirname)):
os.makedirs(os.path.join(os.getcwd(), dirname))
print os.getcwd()
Hey, Many thnx for this useful post..!!
this is simply.
exactly what i was looking for.
can you guide me how to create a Remote Directory ?
Cool Story Bro! super cool banner!
Thanks a lot it helped me.
With havin so much content do you ever run into any problems of plagorism or copyright violation? My site has a lot of unique content I’ve either authored myself or outsourced but it appears a lot of it is popping it up all over the internet without my permission. Do you know any ways to help protect against content from being stolen? I’d truly appreciate it.|
After I originally left a comment I seem to have clicked the -Notify me when new comments are added- checkbox and now every time a comment is added I get four emails with the same comment. There has to be a way you are able to remove me from that service? Appreciate it!|