I am not entirely sure what you want to achieve and if that is the best way, however, the exception you are getting:
TypeError: excepted str, bytes or Os.Pathlike object, not NoneType
is because you are trying to open
output of os.chmod
, which has no return value, if you want to chmod
a file to be able to write to it and then make it read-only again, you can do something like this:
import os
import stat
read_only_file = "1.txt"
read_write_file = "2.txt"
def make_read_only(filename: str) -> None:
os.chmod(filename, stat.S_IREAD)
def make_read_write(filename: str) -> None:
os.chmod(filename, stat.S_IWRITE)
# Read read only file
with open(read_only_file) as f:
data = f.read()
make_read_write(read_write_file)
with open(read_write_file, "w") as f:
f.write(data)
make_read_only(read_write_file)
Bear in mind that this snippet will allow racing the file writeability, as there is a small period in which the file is writeable (race condition) - impact of this "feature" depends on your use case.