What is the python os.access() method? What does os.access do?

乌拉乌拉~
Release: 2018-08-17 14:45:02
Original
2509 people have browsed it

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);
Copy after login

Parameters

## 1.path -- The path to be used to detect whether there is access permission.

2.mode -- mode is F_OK, test the path that exists, or it can be one or more of R_OK, W_OK and X_OK or R_OK, W_OK and X_OK.

3.os.F_OK: As the mode parameter of access(), test whether the path exists.

4.os.R_OK: Contained in the mode parameter of access(), testing whether the path is readable.

5.os.W_OK is included in the mode parameter of access() to test whether the path is writable.

6.os.X_OK is included in the mode parameter of access() to test whether the path is executable.

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
Copy after login

The output result of executing the above program is:

F_OK - 返回值 True
R_OK - 返回值 True
W_OK - 返回值 True
X_OK - 返回值 False
Copy after login
The above is all the content of this article. I hope what I said and the examples I gave can be helpful to you.

For more related knowledge, please visit the

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template