當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python 3 os.fchown()用法及代碼示例


描述

方法fchown()將 fd 給定的文件的所有者和組 ID 更改為數字 uid 和 gid。要保持其中一個 id 不變,請將其設置為 -1。

注意- 此方法在 Python 2.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!!

相關用法


注:本文由純淨天空篩選整理自 Python 3 - os.fchown() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。