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)
引數
返回值
範例
以下範例顯示了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!!