Python的lchmod()
方法將路徑的模式更改為數字模式。如果path
是符號鏈接,則會影響符號鏈接而不是目標。 從Python 3.3開始,它相當於os.chmod(path,mode,follow_symlinks = False)
。
該模式可以採用以下值之一或其按位OR或它們的組合:
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
:由其他人執行
注意 - 此方法已在Python 2.6中引入
語法
以下是lchmod()
方法的語法 -
os.lchmod(path, mode)
參數
- path - 這是要設置哪種模式的檔路徑。
- flags - 這可能需要上述值之一或它們的按位OR組合。
返回值
- 此方法不返回任何值。
示例
以下示例顯示了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 os模組方法
下一篇:
Python異常處理