os.makedirs() can't create a directory with cleared write or list permission bits for owner when parent directories aren't created. This is because for parent directories same mode is used as for final directory.
Note that the mkdir utility creates parent directories with default mode (0o777 & ~umask).
$ mkdir -p -m 0 t1/t2/t3
$ ls -l -d t1 t1/t2 t1/t2/t3
drwxrwxr-x 3 serhiy serhiy 4096 Dec 7 22:30 t1/
drwxrwxr-x 3 serhiy serhiy 4096 Dec 7 22:30 t1/t2/
d--------- 2 serhiy serhiy 4096 Dec 7 22:30 t1/t2/t3/
Traceback (most recent call last):
File "Lib\test\test_os.py", line 907, in test_modeself.assertEqual(stat.S_IMODE(os.stat(parent).st_mode), 0o775)
AssertionError: 511 != 509
Ran 157 tests in 1.865s
FAILED (failures=1, skipped=61)
Traceback (most recent call last):
File "Lib\test\test_os.py", line 2511, in <module>
test_main()
File "C:\Users\vajrasky\Code\cpython\lib\test\support\__init__.py", line 1831,
in decorator
return func(*args)
File "Lib\test\test_os.py", line 2507, in test_main
FDInheritanceTests,
File "C:\Users\vajrasky\Code\cpython\lib\test\support\__init__.py", line 1719,
in run_unittest
_run_suite(suite)
File "C:\Users\vajrasky\Code\cpython\lib\test\support\__init__.py", line 1694,
in _run_suite
raise TestFailed(err)
test.support.TestFailed: Traceback (most recent call last):
File "Lib\test\test_os.py", line 907, in test_modeself.assertEqual(stat.S_IMODE(os.stat(parent).st_mode), 0o775)
AssertionError: 511 != 509
The permission of directory on Windows no matter what mode you give or umask you give to support.temp_umask, is always 0o777 (or 511). I think this test does not make sense in Windows.
I’ve never considered this sort of scenario properly, so I don’t know if leaving the default mode for the parent directories is the best way or not. The obvious but more complicated alternative would be to call chmod() on all the new directories in a second step.
Anyway, if we are going to make any change, it should be documented. And I would say the current patch changes behaviour, so is probably not applicable as a bug fix.
The obvious but more complicated alternative would be to call chmod() on all the new directories in a second step.
This is dangerous, because if you create read-only or unlistable directory, you couldn't remove it without changing the permission of parent directory. shutil.rmtree() would fail.
Activity
serhiy-storchaka commented on Dec 8, 2013
os.makedirs() can't create a directory with cleared write or list permission bits for owner when parent directories aren't created. This is because for parent directories same mode is used as for final directory.
Note that the mkdir utility creates parent directories with default mode (0o777 & ~umask).
The proposed patch emulates the mkdir utility.
See also bpo-19921.
vajrasky commented on Dec 8, 2013
Fails on Windows Vista.
======================================================================
FAIL: test_mode (main.MakedirTests)
----------------------------------------------------------------------
Ran 157 tests in 1.865s
The permission of directory on Windows no matter what mode you give or umask you give to support.temp_umask, is always 0o777 (or 511). I think this test does not make sense in Windows.
Either that, or I am missing something.
serhiy-storchaka commented on Dec 8, 2013
Thank you Vajrasky. Now this check is skipped on Windows.
vadmium commented on Jun 10, 2016
I’ve never considered this sort of scenario properly, so I don’t know if leaving the default mode for the parent directories is the best way or not. The obvious but more complicated alternative would be to call chmod() on all the new directories in a second step.
Anyway, if we are going to make any change, it should be documented. And I would say the current patch changes behaviour, so is probably not applicable as a bug fix.
serhiy-storchaka commented on Jun 10, 2016
This is dangerous, because if you create read-only or unlistable directory, you couldn't remove it without changing the permission of parent directory. shutil.rmtree() would fail.
serhiy-storchaka commented on Mar 24, 2017
New changeset e304e33 by Serhiy Storchaka in branch 'master':
bpo-19930: The mode argument of os.makedirs() no longer affects the file (#799)
e304e33
gpshead commented on Nov 16, 2020
This API change was not strictly a bugfix, it removed a feature existing code was relying on.
https://bugs.python.org/issue42367 opened to reconcile the two.
vanschelven commented on Aug 29, 2025
The discussion now continues at