Python中的OS模塊提供了與操作係統進行交互的函數。操作係統屬於Python的標準實用程序模塊。該模塊提供了使用依賴於操作係統的函數的便攜式方法。
如果文件名和路徑無效或無法訪問,或者具有正確類型但操作係統不接受的其他參數,則os模塊中的所有函數都會引發OSError。
os.lchown()
Python中的方法用於將指定文件路徑的所有者和組ID更改為指定的數字所有者ID(UID)和組ID(GID)。此方法不遵循符號鏈接,等效於os.chown(path, uid, gid, follow_symlinks = False)
方法。
注意: os.lchown()
該方法僅在UNIX平台上可用,並且該方法的函數通常僅對超級用戶或特權用戶可用。
用法: os.lchown(pth, uid, gid)
參數:
路徑:path-like對象,表示要為其設置所有權的he文件路徑。
uid:一個整數值,表示要為文件設置的所有者ID。
id:一個整數值,表示要為文件設置的組ID。
返回類型:此方法不返回任何值。
代碼1:os.lchown()方法的使用
# Python program to explain os.lchown() method
# importing os module
import os
# File path
path = "./file.txt"
# Print the current owner id
# and group id of the file
# os.stat() method will return a
# 'stat_result’ object of
# ‘os.stat_result’ class whose
# 'st_uid' and 'st_gid' attributes
# will represent owner id and group id
# of the file respectively
print("Owner id of the file:", os.stat(path).st_uid)
print("Group id of the file:", os.stat(path).st_gid)
# Change the owner id and
# the group id of the file
# using os.lchown() method
uid = 400
gid = 500
os.lchown(path, uid, gid)
print("\nOwner and group id of the file changed")
# Print the owner id
# and group id of the file
print("\nOwner id of the file:", os.stat(path).st_uid)
print("Group id of the file:", os.stat(path).st_gid)
輸出:
代碼2:使用os.lchown()方法設置任何一個ID,其他保持不變。
# Python program to explain os.lchown() method
# importing os module
import os
# File path
path = "./file.txt"
# Print the current owner id
# and group id of the file
# os.stat() method will return a
# 'stat_result’ object of
# ‘os.stat_result’ class whose
# 'st_uid' and 'st_gid' attributes
# will represent owner id and group id
# of the file respectively
print("Owner id of the file:", os.stat(path).st_uid)
print("Group id of the file:", os.stat(path).st_gid)
# Change only group id of
# the file and leave
# owner id unchanged
# set id as -1 to leave
# it unchanged
uid = 1000
gid = -1
os.lchown(path, uid, gid)
print("\ngroup id of the file changed")
# Print the owner id
# and group id of the file
print("\nOwner id of the file:", os.stat(path).st_uid)
print("Group id of the file:", os.stat(path).st_gid)
輸出:
代碼3:如果指定的路徑是符號鏈接
# Python program to explain os.lchown() method
# importing os module
import os
# File path
path = "./file.txt"
# Creating a symlink
# of the above path
# using os.symlink() method
symlink = "file(symlink).txt"
os.symlink(path, symlink)
# Print the current owner id
# and group id of the file
# as well as the symlink pointing
# to the above specified file path
print("Owner id of the file:", os.stat(path).st_uid)
print("Group id of the file:", os.stat(path).st_gid)
print("Owner id of the symlink:", os.stat(symlink).st_uid)
print("Group id of the symlink:", os.stat(symlink).st_gid)
# Change the owenership
# of the symlink pointing
# to the above file 'file.txt'
uid = 600
gid = 700
os.lchown(symlink, uid, gid)
print("\nOwner id and group id changed")
# Print the owner id
# and group id of the file
# as well as the symlink pointing
# to the above specified file path
print("\nOwner id of the file:", os.stat(path).st_uid)
print("Group id of the file:", os.stat(path).st_gid)
print("Owner id of the symlink:", os.stat(symlink).st_uid)
print("Group id of the symlink:", os.stat(symlink).st_gid)
# As os.lchown() method
# does not follow symlinks
# so, we can not change the
# owner and group id
# through a symlink
輸出:
參考: https://docs.python.org/3/library/os.html
相關用法
- Python set()用法及代碼示例
- Python next()用法及代碼示例
- Python os.dup()用法及代碼示例
- Python PyTorch sin()用法及代碼示例
- Python os.confstr()用法及代碼示例
- Python os.write()用法及代碼示例
- Python os.fchmod()用法及代碼示例
- Python os.read()用法及代碼示例
- Python os.statvfs()用法及代碼示例
- Python sympy.div()用法及代碼示例
- Python os.strerror()用法及代碼示例
- Python os.fstatvfs()用法及代碼示例
- Python os.sysconf()用法及代碼示例
注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | os.lchown() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。