Whats the python way for recursively setting file permissions?

Posted by Geoff on Stack Overflow See other posts from Stack Overflow or by Geoff
Published on 2010-05-17T23:45:17Z Indexed on 2010/05/17 23:50 UTC
Read the original article Hit count: 217

Filed under:
|
|

What's the "python way" to recursively set the owner and group to files in a directory? I could just pass a 'chown -R' command to shell, but I feel like I'm missing something obvious.

I'm mucking about with this:


import os  
path = "/tmp/foo"  
for root, dirs, files in os.walk(path):  
  for momo in dirs:  
    os.chown(momo, 502, 20)

This seems to work for setting the directory, but fails when applied to files. I suspect the files are not getting the whole path, so chown fails since it can't find the files. The error is:

'OSError: [Errno 2] No such file or directory: 'foo.html'

What am I overlooking here?

© Stack Overflow or respective owner

Related posts about python

Related posts about recursive