How to create a folder in python? -mkdir / makedirs doesn't do it right

I have a little problem. I'm a little new to python, so I need help here.

I am trying to create a folder, but it must be location independent.

The user can be on the desktop and do it on the desktop, and if the directory is there.

I mean:

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

not good

Unable to execute:

os.mkdir('//just a folder') ?

Why should I signpost all the way there?

+5
source share
1 answer

Yes, you can transfer only the folder name to os.mkdir, but then it will create this folder in the current working directory. Therefore, you may have to change the current working directory with the user again or again, or simply transfer the entire path to os.mkdirif you do not want to.

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`
+9
source

All Articles