Reputation: 3171
This code raised exception when I tried to create a sub dir ./test/123
under ./test/
. And after examine the permission, I found that dir ./test
created by this code has d-w----r--
, which is strange...If I mkdir in the terminal, that dir will have drwxr-xr-x
permission.
from pathlib import Path
if __name__ == '__main__':
p1 = Path('./test')
p1.mkdir(644, parents=True, exist_ok=True)
p2 = Path('./test/123')
p2.mkdir(644, parents=True, exist_ok=True)
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pathlib.py", line 1267, in mkdir
if not exist_ok or not self.is_dir():
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pathlib.py", line 1358, in is_dir
return S_ISDIR(self.stat().st_mode)
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pathlib.py", line 1168, in stat
return self._accessor.stat(self)
PermissionError: [Errno 13] Permission denied: 'test/123'
Upvotes: 5
Views: 7995
Reputation: 108
Pathlib expects an octal integer instead of decimal. You can denote octal by preficing your mode bits 644
with 0o
, i.e. 0o644
. 644
decimal translates to 1204
in octal which imposes permissions you are seeing there.
Also, to traverse a directory structure you require both read and execute permissions on it, so I would recommend using 0o755
instead of 0o644
.
The Unix command line chmod assumes octal whereas your python pathlib library does not. Hope this helps.
Upvotes: 6