Q: Portable Way to Make Files Read Only

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Efrat Regev

    #1

    Q: Portable Way to Make Files Read Only

    Hello,

    I would like to recurse through a directory and make files (which match
    a specific criteria) read only. From searching the Internet, I found

    which shows how to change file attributes to read only using the
    win32api module. That's great, but I was hoping there's a more portable way
    to do so. Is there a more portable API call to make files read only?

    Many Thanks,

    Efrat


  • Christopher De Vries

    #2
    Re: Q: Portable Way to Make Files Read Only

    On Sun, Feb 13, 2005 at 01:25:02PM -0600, Efrat Regev wrote:[color=blue]
    > I would like to recurse through a directory and make files (which match
    > a specific criteria) read only. From searching the Internet, I found
    > http://aspn.activestate.com/ASPN/Coo.../Recipe/303343
    > which shows how to change file attributes to read only using the
    > win32api module. That's great, but I was hoping there's a more portable way
    > to do so. Is there a more portable API call to make files read only?[/color]

    assuming the variable "path" is set to a string indicating the file in
    question, try the following:

    import os
    import stat

    # get the current file mode
    mode = os.stat(path)[stat.ST_MODE]

    # set it so it is not user, group, or other writable
    os.chmod(path, mode & ~stat.S_IWUSR & ~stat.S_IWGRP & ~stat.S_IWOTH)

    #or

    os.chmod(path,m ode & ~0222)

    Chris

    Comment

    Working...