Python language zero-based introductory tutorial (21)

Python OS file/directory methods

1. Python os.access() method

Overview
The os.access() method attempts to access a path using the current uid/gid. Most operations use effective uid/gid, so the operating environment can be tried in suid/sgid environment.

Grammar
The syntax of the access() method is as follows:

os.access(path, mode);

Parameters
path – The path to check for access.
mode – if mode is F_OK, test for existence of the path, or it can be one or more of R_OK, W_OK and X_OK or R_OK, W_OK and X_OK.
os.F_OK: As the mode parameter of access(), test whether the path exists.
os.R_OK: Included in the mode parameter of access(), it tests whether the path is readable.
os.W_OK Included in the mode parameter of access(), tests whether path is writable.
os.X_OK Included in the mode parameter of access(), tests whether 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

The output of executing the above program is:

F_OK - 返回值 True
R_OK - 返回值 True
W_OK - 返回值 True
X_OK - 返回值 False

2. Python os.chdir() method

Overview
The os.chdir() method is used to change the current working directory to the specified path.

Syntax
The syntax of the chdir() method is as follows:

os.chdir(path)

Parameters
path – The new path to switch to.

Return Value
Returns True if access is allowed, otherwise returns False.

Example
The following example demonstrates the use of the chdir() method:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

path = "/tmp"

# 查看当前工作目录
retval = os.getcwd()
print "当前工作目录为 %s" % retval

# 修改当前工作目录
os.chdir( path )

# 查看修改后的工作目录
retval = os.getcwd()

print "目录修改成功 %s" % retval

The output of executing the above program is:

当前工作目录为 /www
目录修改成功 /tmp

3. Python os.chflags() method

Overview
The os.chflags() method is used to set the path's flags to numeric flags. Multiple tags can be combined using OR.

Only supported under Unix.

Syntax
The syntax of the chflags() method is as follows:

os.chflags(path, flags)

Parameters
path – filename path or directory path.
flags – can be the following values:
stat.UF_NODUMP: non-dump file
stat.UF_IMMUTABLE: file is read-only
stat.UF_APPEND: file can only append content
stat.UF_NOUNLINK: file cannot be deleted
stat.UF_OPAQUE: directory is opaque, requires union Stack view
stat.SF_ARCHIVED: Archiveable files (can be set by superusers)
stat.SF_IMMUTABLE: Files are read-only (can be set by superusers)
stat.SF_APPEND: Files can only append content (can be set by superusers)
stat.SF_NOUNLINK: Files Undeletable (can be set by super user)
stat.SF_SNAPSHOT: Snapshot file (can be set by super user)
return value
This method has no return value.

Example
The following example demonstrates the use of the chflags() method:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os,stat

path = "/tmp/foo.txt"

# 为文件设置标记,使得它不能被重命名和删除
flags = stat.SF_NOUNLINK
retval = os.chflags( path, flags)
print "返回值: %s" % retval

The output of executing the above program is:

返回值: None

4. Python os.chmod() method

Overview
The os.chmod() method is used to change the permissions of a file or directory.

Syntax
The syntax of the chmod() method is as follows:

os.chmod(path, mode)

Parameters
path – filename path or directory path.
flags – It can be generated by bitwise OR operation with the following options. The read permission of the directory means that the list of file names in the directory can be obtained, and the execute permission means that the working directory can be switched to this directory. To delete files in the added directory, you must have both write and execute permissions , file permissions are checked in the order of user id->group id->other, and the first matching permission or prohibition permission is applied.
stat.S_IXOTH: other users have execution rights 0o001
stat.S_IWOTH: other users have write rights 0o002
stat.S_IROTH: other users have read rights 0o004
stat.S_IRWXO: other users have all rights (permission mask) 0o007
stat.S_IXGRP: group User has execute permission 0o010
stat.S_IWGRP: Group user has write permission 0o020
stat.S_IRGRP: Group user has read permission 0o040
stat.S_IRWXG: Group user has full permission (permission mask) 0o070
stat.S_IXUSR: Owner has execute permission 0o100
stat.S_IWUSR: The owner has write permission 0o200
stat.S_IRUSR: The owner has read permission 0o400
stat.S_IRWXU: The owner has full permission (permission mask) 0o700
stat.S_ISVTX: Only the owner can delete and change the file directory in the directory 0o1000
stat.S_ISGID: Execute this file, the effective group of the process is the file owner 0o2000
stat.S_ISUID: Execute this file, the effective user of the process is the file owner 0o4000
stat.S_IREAD: set to read-only under windows
stat.S_IWRITE: cancel read-only under windows
return value
This method has no return value.

Example
The following example demonstrates the use of the chmod() method:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys, stat

# 假定 /tmp/foo.txt 文件存在,设置文件可以通过用户组执行

os.chmod("/tmp/foo.txt", stat.S_IXGRP)

# 设置文件可以被其他用户写入
os.chmod("/tmp/foo.txt", stat.S_IWOTH)

print "修改成功!!"

The output of executing the above program is:

修改成功!!

5. Python os.chown() method

Overview
The os.chown() method is used to change the file owner. If not modified, it can be set to -1. You need superuser privileges to perform permission modification operations.

Only supported under Unix.

Syntax
The syntax of the chown() method is as follows:

os.chown(path, uid, gid);

Parameter
path – file path to set permissions

uid – owning user ID

gid – ID of the user group to which it belongs

Return Value
This method has no return value.

Example
The following example demonstrates the use of the chown() method:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 假定 /tmp/foo.txt 文件存在.
# 设置所有者 ID 为 100
os.chown("/tmp/foo.txt", 100, -1)

print "修改权限成功!!"

The output of executing the above program is:

修改权限成功!!

Guess you like

Origin blog.csdn.net/weixin_44006731/article/details/129060964