I am writing a data processing code, in which I create a new file, write processed data in to this file and close. But the file has to be closed in read-only mode, so that i
This solution preserves previous permission of the file, acting like command chmod -w FILE
chmod -w FILE
import os import stat filename = "path/to/file" mode = os.stat(filename).st_mode ro_mask = 0777 ^ (stat.S_IWRITE | stat.S_IWGRP | stat.S_IWOTH) os.chmod(filename, mode & ro_mask)