Python os.fchown()方法

Python的os.fchown()方法將fd給出的檔的所有者和組ID更改為數字uidgid。 要使其中一個ID不變,請將其設置為-1

注意 - 此方法可用於Python 2.6以上版本。

語法

以下是fchown()方法的語法 -

os.fchown(fd, uid, gid)

參數

  • fd −這是需要設置所有者ID和組ID的檔描述符。
  • uid - 這是要為檔設置的所有者ID。
  • gid - 要為檔設置的組ID。

返回值

  • 此方法不返回任何值,僅適用於Unix操作系統。

示例

以下示例顯示了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!!

上一篇: Python os模組方法 下一篇: Python異常處理