views:

68

answers:

1

I'm working on a Python script that creates text files containing size/space information about the directories that the script is run on. The script needs to be run as root, and as a result, it sets the text files that it creates to root's ownership.

I know I can change the ownership with os.fchown, but how do I pass fchown the uid and gid of the directory that the script is running on?

+1  A: 

Use

import os, stat
info = os.stat(dirpath)
uid, gid = info[stat.ST_UID], info[stat.ST_GID]
Vinay Sajip
Thanks for this. Would I be able to give os.stat() 'os.getcwd()' rather than a static "dirpath"?
steve