How can I in python (3) create a file what others users can write as well. I've so far this but it changes the
os.chmod("/home/pi/test/relaxbank1.txt", 777)
with open("/home/pi/test/relaxbank1.txt", "w") as fh:
fh.write(p1)
what I get
---sr-S--t 1 root root 12 Apr 20 13:21 relaxbank1.txt
expected (after doing in commandline $ sudo chmod 777 relaxbank1.txt )
-rwxrwxrwx 1 root root 12 Apr 20 13:21 relaxbank1.txt
This is a robust method
See how:
stat.S_IWOTH
is used instead of the raw binary constant, which is more semantic+o
to the file, and use theumask
by default, see also: How do you do a simple "chmod +x" from within python?See also: Write file with specific permissions in Python
The problem is your call to
open()
recreates the call. Either you need to move thechmod()
to after you close the file, OR change the file mode tow+
.Option1:
Option2:
Comment: Option1 is slightly better as it handles the condition where the file may not already exist (in which case the
os.chmod()
will throw an exception).If you don't want to use
os.chmod
and prefer to have the file created with appropriate permissions, then you may useos.open
to create the appropriate file descriptor and thenopen
the descriptor: