Python | os。chmod法

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

null

语法:

os.chmod(path, mode)

参数: path–文件或目录路径的路径名 模式—— 模式 可以采用以下值之一:

  • 统计数据: 在执行时设置用户ID
  • 统计S_ISGID: 在执行时设置组ID
  • 统计数据: 强制执行记录锁定
  • 统计S_ISVTX: 执行后保存文本图像
  • 统计S_IREAD: 由所有者阅读。
  • 统计S_IWRITE: 由所有者撰写。
  • 统计S_IEXEC: 由所有者执行。
  • 统计S_IRWXU: 由所有者读取、写入和执行
  • 统计S_IRUSR: 所有者阅读
  • 统计S_IWUSR: 由所有者撰写。
  • 统计S_IXUSR: 由所有者执行。
  • 统计S_IRWXG: 按组读、写和执行
  • 统计S_IRGRP: 分组阅读
  • 统计S_IWGRP: 分组写作
  • 统计S_IXGRP: 分组执行
  • 统计S_IRWXO: 由他人读、写和执行。
  • 统计S_IROTH: 被他人阅读
  • 统计S_IWOTH: 别人写的
  • 统计S_IXOTH: 被他人处决

代码#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.
© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享