os.fchown(fd, uid, gid)
fd -- 這是其所有者ID和組ID需要進行設定的檔案描述符
uid -- 這是要設定的檔案所有者ID
gid -- 這是要設定檔案的組ID。
#!/usr/bin/python3 import os, sys, stat # Now open a file "/tmp/foo.txt" fd = os.open( "/tmp", os.O_RDONLY ) # Set the user Id to 100 for this file. os.fchown( fd, 100, -1) # Set the group Id to 50 for this file. os.fchown( fd, -1, 50) print ("Changed ownership successfully!!") # Close opened file. os.close( fd )
Changed ownership successfully!!