Python Forum
Set permnission with makedirs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Set permnission with makedirs
#1
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 ?
[Image: NfRQr9R.jpg]
Reply
#2
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?
Reply
#3
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 ?
[Image: NfRQr9R.jpg]
Reply
#4
Yes it's linked to the umask, is there a way to not apply the umask ?
[Image: NfRQr9R.jpg]
Reply
#5
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
[Image: NfRQr9R.jpg]
Reply
#6
(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
Reply
#7
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 !?
[Image: NfRQr9R.jpg]
Reply
#8
(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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020