Python的os.fchown()
方法將fd
給出的檔案的所有者和組ID更改為數位uid
和gid
。 要使其中一個ID不變,請將其設定為-1
。
注意 - 此方法可用於Python 2.6以上版本。
語法
以下是fchown()
方法的語法 -
os.fchown(fd, uid, gid)
引數
返回值
範例
以下範例顯示了fchmod()
方法的用法。
#!/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!!