Python os.fchown()方法

2019-10-16 23:04:23

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!!