For this you use os.chmod
import os from stat import S_IREAD, S_IRGRP, S_IROTH filename = "path/to/file" os.chmod(filename, S_IREAD|S_IRGRP|S_IROTH)
Please note that this assumes that you have the appropriate permissions and that you want the owner to be able to read this file. Remove S_IROTH and S_IRGRP if it is not.
UPDATE
If you need to write the file again, just call os.chmod as follows:
from stat import S_IWUSR
Just call this before you open the file for writing, then call the first form to make it read-only after you are done.
aruisdante
source share