Friday, August 29, 2008

Read-only Windows files with Python

How do you use Python to get or change read-only/writeable access on files in Windows? The Python docs don't answer this in a direct manner. Here's one option using only the standard library.

import os, stat
myFile = r'C:\stuff\grail.txt'

fileAtt = os.stat(myFile)[0]
if (not fileAtt & stat.S_IWRITE):
   # File is read-only, so make it writeable
   os.chmod(myFile, stat.S_IWRITE)
else:
   # File is writeable, so make it read-only
   os.chmod(myFile, stat.S_IREAD)
You may prefer the pywin32 extensions for this sort of thing...
import win32api, win32con
myFile = r'C:\stuff\grail.txt'

fileAtt = win32api.GetFileAttributes(myFile)
if (fileAtt & win32con.FILE_ATTRIBUTE_READONLY):
   # File is read-only, so make it writeable
   win32api.SetFileAttributes(myFile, ~win32con.FILE_ATTRIBUTE_READONLY)
else:
   # File is writeable, so make it read-only
   win32api.SetFileAttributes(myFile, win32con.FILE_ATTRIBUTE_READONLY)
Or, more concisely with win32:
roAtt = win32api.GetFileAttributes(myFile) & win32con.FILE_ATTRIBUTE_READONLY
win32api.SetFileAttributes(myFile, ~roAtt)
Using win32 you can also set other Windows file attributes (unlike os.chmod), but read/write is usually all I care about.

7 comments:

Anonymous said...

You can use the dos attrib command:

>>>os.system('attrib -R %s' % myFile)

Tyler Hurd said...
This comment has been removed by the author.
Tyler Hurd said...
This comment has been removed by the author.
Tyler Hurd said...

Third time's a charm... heh.
I wanted a single line read-only check and I ended up with this:

import os

if os.access('C:/temp/test.txt',W_OK) :
    print 'File is NOT read only'
else :
    print 'File is read only!'

This seems to make more sense to me for whatever reason.

Tyler Hurd said...

sigh... I'm not deleting any more posts. That should read:

if os.access('C:/temp/test.txt',os.W_OK) :

Anonymous said...

Python docs are frustrating in this regard, your example here has _really_ helped me! Thank you :)

Gia said...

Good share