Read/Write files in Python
I need to make a file readable and writable in python. Currently the开发者_如何学Python file is read-only. I am running on a Windows machine. I run the following code:
os.chmod(projectPath, stat.S_IWRITE | stat.S_IREAD)
on a file that needs to be read/write. But when I try to execute the file that needs to be read write, I get the following:
ISDEV : fatal error -2200: Could not overwrite file C:\WINDOWS\Temp\STixInstaller\STixInstallShield.ism
So obviously, it is not making the file read/write. I then check the file permissions and it is still read-only.
Any ideas why this fails or if there is an easier way to do this I am missing?
I think you only need the stat.S_IWRITE mode. I just ran a test with this code
def main():
path = "C:\\temp\\log.txt"
os.chmod(path, stat.S_IWRITE)
And it set a file that was read only to not read only, where as when I ran it with S_IREAD instead, it set it back to read only
Make sure you have permissions to change the file. Who is the owner of the file? Is it the one who runs the Python script? All these have to be taken into account.
精彩评论