Python Forum

Full Version: Set permnission with makedirs
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,

I would like to set the permission when using
from os import makedirs
I would like to have (for a directory) d-rwx-r-r (in other for "others") to be able to read the files inside that folder.

When I do a stat on a folder that have the permission that I want I get
Output:
(0755/drwxr-xr-x)
When I transpose this in python -->
makedirs('/somewhere', mode=0o0755)
The created folder give me another permission than expected..
Output:
(0750/drwxr-x---)
Any ideas ?
I try the same on my computer and it works
Output:
λ python Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from os import makedirs >>> makedirs('somewhere', mode=0o0755) >>> λ stat somewhere File: somewhere Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 804h/2052d Inode: 19141748 Links: 2 Access: (0755/drwxr-xr-x) Uid: ( 1000/ eric) Gid: ( 1000/ eric) Access: 2023-06-06 14:47:15.152311228 +0200 Modify: 2023-06-06 14:47:15.152311228 +0200 Change: 2023-06-06 14:47:15.152311228 +0200 Birth: 2023-06-06 14:47:15.152311228 +0200 λ rmdir somewhere λ
The only difference is that I created somewhere and not /somewhere. Are you doing this as super-user?
Thank you so much @Gribouillis to have tried !

Quote:Are you doing this as super-user?
no, but I just tried with super-user also and it give me the same 750 instead of 755.

I suppose it's linked to my umask ?
Yes it's linked to the umask, is there a way to not apply the umask ?
Meanwhile I do a chmod afterward,

but it so weird ! where I do a stat after the chmod I got 0755 as expected.

but when I display the Permissions trough Thunar I get
[Image: L0Bo5IP.png]

Huh
(Jun-06-2023, 01:15 PM)SpongeB0B Wrote: [ -> ]Yes it's linked to the umask, is there a way to not apply the umask ?
You could perhaps call os.umask() with suitable arguments? Try this to see the umask
Output:
λ python -c "import os; print(os.umask(0))" 2
Thanks @Gribouillis !

I'm shocked the documentation about os.umask() is quite not explanatory !

So according to the documentation
Set the current numeric umask and return the previous umask.
print(os.umask(0))
Should not only display the umask but also set it !?
(Jun-15-2023, 07:47 AM)SpongeB0B Wrote: [ -> ]Should not only display the umask but also set it !?
I just wanted to show the default value of the umask on my system. I chose the value 0 at random. Choose the value that you actually need for your file operation.

In the linux console, the current umask can also be displayed by invoking the umask command without argument

Output:
λ umask 0002
You could perhaps try
os.umask(os.umask(0) & ~(0o005))
to make sure that the umask does not prevent the "others" permission 5, or even
os.umask(os.umask(0) & ~(0o0755))
to make sure that the umask doesn't disallow the mode 0755