In today’s article, let’s take a look at the python os.access() method. In this article, we will learn about the python os.acces method, which you may have never seen before, and understand its definition and function. Without further ado, let’s start learning.
Overview
The os.access() method uses the current uid/gid to try to access the path. Most operations use valid uid/gid, so the runtime environment can be tried in a suid/sgid environment.
Grammar
The access() method syntax format is as follows:
os.access(path, mode);
Parameters
Return value
Returns True if access is allowed, otherwise returns False.Example
The following example demonstrates the use of the access() method:#!/usr/bin/python # -*- coding: UTF-8 -*- 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
F_OK - 返回值 True R_OK - 返回值 True W_OK - 返回值 True X_OK - 返回值 False
Python tutorial column on the php Chinese website.
The above is the detailed content of What is the python os.access() method? What does os.access do?. For more information, please follow other related articles on the PHP Chinese website!