Turn Read-Only flag off on Win files?
Pete Shinners
pshinners at mediaone.net
Sat Sep 30 12:20:27 EDT 2000
"Bob Hyatt" <rhhyatt at charter.net> wrote
> I want to point the program at a directory on a Win98 Disk, and for
> each file in all the directories under this directory, turn off the
> read-only flag.
>
> I thought this would be an easy item, but I find I can't even get
> started. I am completely confused as to how to:
>
> A. Tell it to turn off the read-only file attribute
> B. Loop through all files in the directory
> C. Loop through all directories under the primary directory
to change the readonly attribute on windows, there
are a couple of routes you'll need to test out.
if you have a variable 'filename' with the path
to a file, you can check for readonly like this.
if not os.access(filename, os.R_OK):
print filename, 'is read only!'
to change the permissions, you need to use the
os.chmod command. chmod is a unix command to change
permissions on a file, but it says it works under
windows. a mode of 0777 will give full access to a
file under windows, so i assume that will work for
windows too?
os.chmod(filename, 0777)
if that doesn't appear to be working, you can do it
the old fashioned way.
os.system('attrib -r '+filename)
that will call the attrib command in dos to remove
the readonly flag on a file
as for getting around in the directories, your best
bet is the os.path.walk, which will call a function
of yours with all the information it needs. here is
a quick sample that should help your project
import os
def walk_callback(arg, d, files):
for f in files:
filename = os.path.join(d, f)
if not os.access(filename, os.R_OK):
print filename, 'is readonly'
os.path.walk('c:\\', walk_callback, None)
good luck, bob. have fun with python!
More information about the Python-list
mailing list