简体   繁体   中英

Read/Write files in Python

I need to make a file readable and writable in python. Currently the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM