📜  Python| os.chmod 方法

📅  最后修改于: 2022-05-13 01:55:50.004000             🧑  作者: Mango

Python| os.chmod 方法

Python中的os.chmod()方法用于将路径模式更改为数字模式。

句法:

os.chmod(path, mode)

代码#1:

# Python program to explain os.chmod() method
  
# importing necessary libraries
import os, sys, stat
  
# Set given file read by the owner.
os.chmod("/Geeks/gfg.txt", stat.S_IREAD)
print("File can be read only by owner.")
  
# Set given file read by others.
os.chmod("/Geeks/gfg.txt", stat.S_IROTH)
print("File access changed, can be read by others now.")

输出:

File can be read only by owner.
File access changed, can be read by others now.


代码#2:

# Python program to explain os.chmod() method
  
# importing necessary libraries
import os, sys, stat
  
# Set given file written by the owner.
os.chmod("/Geeks/gfg.txt", stat.S_IWRITE)
  
# Set given file executed by the owner.
os.chmod("/Geeks/gfg.txt", stat.S_IXUSR)
print("File can be written and executed only by owner.")

输出:

File can be written and executed only by owner.