os.makedirs doesn't understand in my path
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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
Related reading
- Output Dstream of Apache Spark in Python
- Override Python's 'in' operator?
- Override S3 endpoint using Boto3 configuration file
- Paginating a DynamoDB query in boto3
- pandas - add new column to dataframe from dictionary
- Pandas column of lists, create a row for each list element
- Pandas convert dataframe to array of tuples
- Pandas 'countdistinct' equivalent
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.