Python os.fchmod()方法

2019-10-16 23:04:22

Python的os.fchmod()方法將fd給出的檔案的模式更改為數位模式。該模式可以採用以下值之一或按位元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以上版本。

語法

以下是fchmod()方法的語法 -

os.fchmod(fd, mode)

引數

  • fd ? 這是將設定哪種模式的檔案描述符。
  • mode ? 這可能需要上述值之一或按位元OR或它們的組合。

返回值

  • 此方法不返回任何值。僅適用於Unix作業系統。

範例

以下範例顯示了fchmod()方法的用法。

#!/usr/bin/python3
import os, sys, stat

# Now open a file "/tmp/foo.txt"
fd = os.open( "/tmp", os.O_RDONLY )

# Set a file execute by the group.

os.fchmod( fd, stat.S_IXGRP)

# Set a file write by others.
os.fchmod(fd, stat.S_IWOTH)

print ("Changed mode successfully!!")

# Close opened file.
os.close( fd )

執行上面程式碼後,將得到以下結果 -

Changed mode successfully!!