os.makedirs doesn't understand in my path
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
os.makedirs() does not expand the ~ (tilde) character because tilde expansion is a shell feature, not a filesystem feature. Python's file operations treat ~ as a literal directory name. To resolve ~ to the user's home directory, call os.path.expanduser() before passing the path to os.makedirs(). This applies to all os and open() calls, not just makedirs.
The Problem
The shell expands ~ to /home/username when you type mkdir ~/projects, but Python does not.
The Fix: os.path.expanduser()
Always call expanduser() before any file operation that receives a user-provided path.
Using pathlib (Modern Approach)
pathlib.Path.expanduser() is the modern replacement for os.path.expanduser().
Expanding Environment Variables Too
Paths may also contain environment variables like $HOME or %USERPROFILE%:
Other Functions Affected
~ is not expanded by any Python file function — not just makedirs:
Getting the Home Directory Directly
Cross-Platform Path Building
Common Pitfalls
- Assuming
~works in Python like it does in the shell: The shell expands~before the program sees it. Python receives the raw string. Always callexpanduser()or usePath.expanduser()for paths containing~. - Creating a literal
~directory: If you callos.makedirs("~/something")without expanding, Python creates a directory literally named~in the current working directory. This is confusing and hard to delete (userm -rf './~'with quotes). - Forgetting
expandvars()for$HOME:expanduser()only handles~. Environment variables like $HOMEor$USERrequireos.path.expandvars(). Chain both for maximum compatibility. - Hardcoding
/home/username: Hardcoded home paths break on other machines and operating systems. Always usePath.home(),os.path.expanduser("~"), oros.environ["HOME"]. - Not using
exist_ok=True:os.makedirs()raisesFileExistsErrorif the directory already exists (default behavior). Always passexist_ok=Trueunless you specifically want to fail on existing directories.
Summary
os.makedirs()does not expand~— it creates a literal directory named~- Call
os.path.expanduser(path)orPath(path).expanduser()before any file operation - Use
Path.home()to get the home directory without string manipulation expanduser()handles~,expandvars()handles$HOME— chain both for full expansion- This applies to all Python file functions:
open(),os.path.exists(),os.listdir(), etc. - Prefer
pathlib.Pathoveros.pathfor modern, readable path handling

