My python script creates a file if it doesn't exist, reads and writes that file. The script may be run by root (automated) or by the user (refresh request). I need the file to be created with write permission so that in both cases the file can be re-written.
import os
f = os.open('file', os.O_CREAT, 0777)
os.close(f)
but then...
$ ls -l
-rwxr-xr-x 1 pi pi 0 Feb 22 13:51 file
However, this script works and I don't understand the difference:
import os
f = os.open('file', os.O_CREAT)
os.fchmod(f, 0777)
os.close(f)
...and then:
$ ls -l
-rwxrwxrwx 1 pi pi 0 Feb 22 13:54 file
You're not setting umask, you're setting the file mode bits, which are masked by the umask. Per the documentation:
Open the file file and set various flags according to flags and possibly its mode according to mode. The default mode is 0777 (octal), and the current umask value is first masked out. ...
Your umask value appears to be 0022, thus masking out group and other user write permissions.
This
os.fchmod(f, 0777)
explicitly sets the file permissions to 0777 despite the umask value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With