Turn Read-Only flag off on Win files?

Bob Hyatt rhhyatt at charter.net
Sat Sep 30 13:50:51 EDT 2000


Pete,

Thanks for the hints, The final result:

import os
def walk_callback(arg, dirname, files):
    for f in files:
        filename = os.path.join(dirname, f)
        if not os.access(filename, os.W_OK):
            os.chmod(filename, 0777)
os.path.walk('H:\', walk_callback, None)

Now, this is what I call a great language! I know more than 20 other
computer languages. All have their strengths and weaknesses. I am always
looking for another language that 'enables' me and my time.

I really appreciate the quick response and I am very impressed with Python.
I have three of the books, yet when I went looking for the information I
asked here, I found it difficult to discern what 'words and syntax' applied.
Now, thanks to your example, the OS. group means a lot more to me.

Regards, Bob Hyatt


"Pete Shinners" <pshinners at mediaone.net> wrote in message
news:fpoB5.31146$jD2.4183927 at typhoon.we.rr.com...
>
> "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