小白教程
所有教程
关于
Search
172.69.6.177
172.69.6.177
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Python3 os access”的源代码
本页内容
下一节:
Python3_os_chdir
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:os.access(path, mode)}}[[Category:python3 os file methods|1]] = Python3 os.access() 方法 = [[python3 os file methods|Python3 OS 文件/目录方法]] === 概述 === os.access() 方法使用当前的uid/gid尝试访问路径。大部分操作使用有效的 uid/gid, 因此运行环境可以在 suid/sgid 环境尝试。 === 语法 === '''access()'''方法语法格式如下: <sample title="" desc="" lang="python" hererun="1"> os.access(path, mode); </sample> === 参数 === * '''path''' -- 要用来检测是否有访问权限的路径。 * '''mode''' -- mode为F_OK,测试存在的路径,或者它可以是包含R_OK, W_OK和X_OK或者R_OK, W_OK和X_OK其中之一或者更多。 ** '''os.F_OK:''' 作为access()的mode参数,测试path是否存在。 ** '''os.R_OK:''' 包含在access()的mode参数中 , 测试path是否可读。 ** '''os.W_OK''' 包含在access()的mode参数中 , 测试path是否可写。 ** '''os.X_OK''' 包含在access()的mode参数中 ,测试path是否可执行。 === 返回值 === 如果允许访问返回 True , 否则返回False。 == 以下实例演示了 access() 方法的使用: <sample title="" desc="" lang="python" hererun="1"> #!/usr/bin/python3 import os, sys # 假定 /tmp/foo.txt 文件存在,并有读写权限 ret = os.access("/tmp/foo.txt", os.F_OK) print ("F_OK - 返回值 %s"% ret) ret = os.access("/tmp/foo.txt", os.R_OK) print ("R_OK - 返回值 %s"% ret) ret = os.access("/tmp/foo.txt", os.W_OK) print ("W_OK - 返回值 %s"% ret) ret = os.access("/tmp/foo.txt", os.X_OK) print ("X_OK - 返回值 %s"% ret) </sample> 执行以上程序输出结果为: <sample title="" desc="" lang="python" hererun="1"> F_OK - 返回值 True R_OK - 返回值 True W_OK - 返回值 True X_OK - 返回值 False </sample> [[python3 os file methods|Python3 OS 文件/目录方法]]
返回至“
Python3 os access
”。
下一节:
Python3_os_chdir