Reputation: 2550
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
Upvotes: 51
Views: 149591
Reputation: 12022
For python 3.4+ you can use pathlib as follows
import os
from pathlib import Path
os.umask(0) # get full permission spectrum
filepath = Path("/path/to/file.txt")
filepath.touch(mode=0o777)
filepath.write_text("hi")
Upvotes: 1
Reputation: 9096
If you don't want to use os.chmod
and prefer to have the file created with appropriate permissions, then you may use os.open
to create the appropriate file descriptor and then open
the descriptor:
import os
# The default umask is 0o22 which turns off write permission of group and others
os.umask(0)
descriptor = os.open(
path='filepath',
flags=(
os.O_WRONLY # access mode: write only
| os.O_CREAT # create if not exists
| os.O_TRUNC # truncate the file to zero
),
mode=0o777
)
with open(descriptor, 'w') as fh:
fh.write('some text')
# the descriptor is automatically closed when fh is closed
Using a custom opener
will make things easier and less error-prone. open
will generate the appropriate flags
for our opener
according to the requested mode (w
):
import os
os.umask(0)
def opener(path, flags):
return os.open(path, flags, 0o777)
with open('filepath', 'w', opener=opener) as fh:
fh.write('some text')
Python 2 Note:
The built-in open() in Python 2.x doesn't support opening by file descriptor. Use os.fdopen
instead; otherwise you'll get:
TypeError: coercing to Unicode: need string or buffer, int found.
Upvotes: 115
Reputation: 1
In addition to providing write permission to others: OTH, you can also add write permissions for group: GRP. Note what hierarchy you want to assign what permissions to while writing a file.
st = os.stat(path)
os.chmod(path, st.st_mode | stat.S_IWOTH | stat.S_IWGRP)
Upvotes: -1
Reputation: 2249
Similar to AXO's idea where it's preferable to have the file created with the correct permissions, the opener
argument of open()
seems quite handy for this purpose.
import os
def opener(path, flags):
return os.open(path, flags, 0o777)
os.umask(0) # Without this, the created file will have 0o777 - 0o022 (default umask) = 0o755 permissions
with open("myfile.txt", "w", opener=opener) as f:
f.write("myline")
From the documentation for opener
:
A custom opener can be used by passing a callable as opener. The underlying file descriptor for the file object is then obtained by calling opener with (file, flags). opener must return an open file descriptor (passing os.open as opener results in functionality similar to passing None).
Upvotes: 13
Reputation: 384344
This is a robust method
#!/usr/bin/env python3
import stat
import os
path = 'outfile.txt'
with open(path, 'w') as fh:
fh.write('blabla\n')
st = os.stat(path)
os.chmod(path, st.st_mode | stat.S_IWOTH)
See how:
stat.S_IWOTH
is used instead of the raw binary constant, which is more semantic+o
to the file, and use the umask
by default, see also: How do you do a simple "chmod +x" from within python?See also: Write file with specific permissions in Python
Upvotes: 3
Reputation: 11738
The problem is your call to open()
recreates the call. Either you need to move the chmod()
to after you close the file, OR change the file mode to w+
.
Option1:
with open("/home/pi/test/relaxbank1.txt", "w+") as fh:
fh.write(p1)
os.chmod("/home/pi/test/relaxbank1.txt", 0o777)
Option2:
os.chmod("/home/pi/test/relaxbank1.txt", 0o777)
with open("/home/pi/test/relaxbank1.txt", "w+") as fh:
fh.write(p1)
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).
Upvotes: 30