描述
方法lchown()將路徑的所有者和組 ID 更改為數字 uid 和 gid。此函數不會遵循符號鏈接。要保持其中一個 id 不變,請將其設置為 -1。從 Python 3.3 開始,這相當於 os.chown(path, uid, gid, follow_symlinks=False)。
用法
以下是語法lchown()方法 -
os.lchown(path, uid, gid)
參數
path- 這是要為其設置所有權的文件路徑。
uid- 這是要為文件設置的所有者 ID。
gid- 這是要為文件設置的組 ID。
返回值
此方法不返回任何值。
示例
下麵的例子展示了 lchown() 方法的用法。
#!/usr/bin/python3
import os, sys
# Open a file
path = "/var/www/html/foo.txt"
fd = os.open( path, os.O_RDWR|os.O_CREAT )
# Close opened file
os.close( fd )
# Now change the file ownership.
# Set a file owner ID
os.lchown( path, 500, -1)
# Set a file group ID
os.lchown( path, -1, 500)
print ("Changed ownership successfully!!")
當我們運行上述程序時,它會產生以下結果 -
Changed ownership successfully!!
相關用法
- Python 3 os.lchmod()用法及代碼示例
- Python 3 os.lchflags()用法及代碼示例
- Python 3 os.lstat()用法及代碼示例
- Python 3 os.lseek()用法及代碼示例
- Python 3 os.link()用法及代碼示例
- Python 3 os.listdir()用法及代碼示例
- Python 3 os.fstatvfs()用法及代碼示例
- Python 3 os.minor()用法及代碼示例
- Python 3 os.close()用法及代碼示例
- Python 3 os.unlink()用法及代碼示例
- Python 3 os.major()用法及代碼示例
- Python 3 os.rmdir()用法及代碼示例
- Python 3 os.fdopen()用法及代碼示例
- Python 3 os.fdatasync()用法及代碼示例
- Python 3 os.isatty()用法及代碼示例
- Python 3 os.rename()用法及代碼示例
- Python 3 os.walk()用法及代碼示例
- Python 3 os.renames()用法及代碼示例
- Python 3 os.makedirs()用法及代碼示例
- Python 3 os.utime()用法及代碼示例
注:本文由純淨天空篩選整理自 Python 3 - os.lchown() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。