I think I am pretty familiar with how os.mkdir
and os.makedirs
work but somehow I'm missing something here. I have a function named check_for_dir()
in my script myscript.py
that is located in this path Users/myuser/Projects/myProject/myscript.py
. I want to create a directory named mydirectory
in my home folder. It looks like this:
import os
path = 'Users/myuser/mydirectory/'
def check_for_dir(path):
if not os.path.exists(path):
os.makedirs(path)
check_for_dir(path)
But for some reason the whole structure defined in variable path
is created in script's location. What this means is that directory mydirectory/
is created in this path:
Users/myuser/Projects/myproject/Users/myuser/mydirectory/
What am I doing wrong?
import os
path = '/Users/myuser/mydirectory/'
def check_for_dir(path):
if not os.path.exists(path):
os.makedirs(path)
check_for_dir(path)
I think you have to change path as path = '/Users/myuser/mydirectory/' Hope this will help
If you specify path = 'Users/myuser/mydirectory/' then it will be consider as a relative path then creates a folder as you have mentioned in the question