fchown()方法更改通過 df 給定數字UID和GID檔的所有者和組ID。如要退出ID時不變,可將其設置為-1。
注意:此方法是在 Python2.6 起可用的。
語法
以下是 fchown() 方法的語法:
os.fchown(fd, uid, gid)
參數
-
fd -- 這是其所有者ID和組ID需要進行設置的檔描述符
-
uid -- 這是要設置的檔所有者ID
-
gid -- 這是要設置檔的組ID。
返回值
此方法不返回任何值。只在類 Unix 操作系統上可用。
示例
下麵的示例顯示 fchown()方法的使用。
#!/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!!
上一篇:
Python3檔方法
下一篇:
Python3 os檔目錄的方法