描述
方法chmod()將路徑模式更改為傳遞的數字模式。該模式可以采用以下值之一或它們的按位或組合 -
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- 由他人撰寫。
stat.S_IXOTH− 由他人執行。
用法
以下是語法chmod()方法 -
os.chmod(path, mode)
參數
path- 這是將設置模式的路徑。
mode- 這可能采用上述值之一或它們的按位或組合。
返回值
此方法不返回任何值。
注意− 盡管 Windows 支持 chmod(),您隻能用它設置文件的隻讀標誌(通過 stat.S_IWRITE 和 stat.S_IREAD 常量或相應的整數值)。所有其他位都被忽略。
示例
下麵的例子展示了 chmod() 方法的用法。
#!/usr/bin/python3
import os, sys, stat
# Assuming /tmp/foo.txt exists, Set a file execute by the group.
os.chmod("/tmp/foo.txt", stat.S_IXGRP)
# Set a file write by others.
os.chmod("/tmp/foo.txt", stat.S_IWOTH)
print ("Changed mode successfully!!")
結果
當我們運行上述程序時,它會產生以下結果 -
Changed mode successfully!!
相關用法
- Python 3 os.chflags()用法及代碼示例
- Python 3 os.chroot()用法及代碼示例
- Python 3 os.chdir()用法及代碼示例
- Python 3 os.chown()用法及代碼示例
- Python 3 os.close()用法及代碼示例
- Python 3 os.closerange()用法及代碼示例
- Python 3 os.fstatvfs()用法及代碼示例
- Python 3 os.minor()用法及代碼示例
- 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.tcgetpgrp()用法及代碼示例
注:本文由純淨天空篩選整理自 Python 3 - os.chmod() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。