描述
方法lchmod()將路徑模式更改為數字模式。如果 path 是一個符號鏈接,這會影響符號鏈接而不是目標。從 Python 3.3 開始,這等效於 os.chmod(path, mode, follow_symlinks=False)。
該模式可以采用以下值之一或它們的按位或組合:
stat.S_ISUID:在執行時設置用戶 ID。
stat.S_ISGID:在執行時設置組 ID。
stat.S_ENFMT:強製記錄鎖定。
stat.S_ISVTX:執行後保存文本圖像。
stat.S_IREAD:由業主閱讀。
stat.S_IWRITE:樓主寫的。
stat.S_IEXEC:由所有者執行。
stat.S_IRWXU:由所有者讀取、寫入和執行。
stat.S_IRUSR:由業主閱讀。
stat.S_IWUSR:樓主寫的。
stat.S_IXUSR:由所有者執行。
stat.S_IRWXG:按組讀取、寫入和執行。
stat.S_IRGRP:分組閱讀。
stat.S_IWGRP:按組寫。
stat.S_IXGRP:按組執行。
stat.S_IRWXO:由其他人讀取、寫入和執行。
stat.S_IROTH:被他人閱讀。
stat.S_IWOTH: other 寫的。
stat.S_IXOTH:由他人執行。
注意:這個方法已經在 Python 2.6 中引入了
用法
以下是語法lchmod()方法:
os.lchmod(path, mode)
參數
path─ 這是要設置模式的文件路徑。
mode─ 這可能采用上述值之一或它們的按位或組合。
返回值
此方法不返回任何值。
示例
下麵的例子展示了 lchmod() 方法的用法。
#!/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 mode.
# Set a file execute by group.
os.lchmod( path, stat.S_IXGRP)
# Set a file write by others.
os.lchmod("/tmp/foo.txt", stat.S_IWOTH)
print ("Changed mode successfully!!")
當我們運行上麵的程序時,它會產生以下結果:
Changed mode successfully!!
相關用法
- Python 3 os.lchown()用法及代碼示例
- 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.lchmod() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。