How to make a folder in python? -mkdir/makedirs doesn't do this right
Asked Answered
W

2

10

I have a little problem. I'm kinda new in python so I need help here.

I'm trying to make a folder but it should be independent on location.

The user can be on desktop and it will make on desktop and if in a directiory there and so.

I mean:

os.mkdir('C:\\Program Files\\....') 

is not good

It's not possible to do:

os.mkdir('\\just a dir') ?

Why do I have to mention all the way to there?

Walsingham answered 25/4, 2013 at 9:55 Comment(0)
A
16

Yes you can pass only the folder name to os.mkdir but then it'll create that folder in the current working directory. So you may have to change the current working directory again and again with the user or simply pass the whole path to os.mkdir if you don't want to do that.

In [13]: import os

In [14]: os.getcwd()
Out[14]: '/home/monty'

In [15]: os.mkdir("foo")  #creates foo in /home/monty

In [17]: os.chdir("foo") #change the current working diirectory to `foo`

In [19]: os.getcwd()
Out[19]: '/home/monty/foo'

In [18]: os.mkdir("bar")  #now `bar` is created in `/home/monty/foo`
Acicular answered 25/4, 2013 at 10:4 Comment(0)
T
0

os.mkdir(filepath+directory_name) This command creates the directory "directory_name" in the path mentioned in mkdir filepath="C:\Users" os.mkdir(filepath+"\"+

Tridactyl answered 18/4, 2021 at 3:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.