Q: Portable Way to Make Files Read Only

E

Efrat Regev

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
http://aspn.activestate.com/ASPN/Cookbook/Python/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?

Many Thanks,

Efrat
 
C

Christopher De Vries

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/Cookbook/Python/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?

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,mode & ~0222)

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top