Python os.chown()方法

2019-10-16 23:04:13

Python的os.chown()方法將路徑的所有者和組ID更改為數位uidgid。 要使其中一個ID不變,請將其設定為-1。要設定所有權,需要超級使用者許可權。

語法

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

os.chown(path, uid, gid)

引數

  • path ? 這是需要設定所有者ID和組ID的路徑。
  • uid ? 這是為檔案設定的所有者ID。
  • gid ? 這是為檔案設定的組ID。

返回值

  • 此方法不返回任何值。

範例

以下範例顯示了chown()方法的用法。

#!/usr/bin/python3
import os, sys

# Assuming /tmp/foo.txt exists.

# To set owner ID 100 following has to be done.
os.chown("/tmp/foo.txt", 100, -1)

print ("Changed ownership successfully!!")

執行上面程式碼後,將得到以下結果 -

Changed ownership successfully!!