Python| os.umask() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.umask()
方法用于设置当前数字umask值并获取之前的umask值。
umask代表用户文件创建模式掩码。这用于确定新创建的文件或目录的文件权限。
Syntax: os.umask(mask)
Parameter:
mask: An integer value denoting a valid umask value.
Return Type: This method sets the current umask value and returns an integer value which represents the previous umask value.
代码 #1:使用 os.umask() 方法
# Python program to explain os.umask() method
# importing os module
import os
# mask
# 18 in decimal is
# equal to 0o022 in octal
mask = 18
# Set the current umask value
# and get the previous
# umask value
umask = os.umask(mask)
# Print the
# current and previous
# umask value
print("Current umask:", mask)
print("Previous umask:", umask)
输出:
Current umask: 18
Previous umask: 54
代码 #2:在 os.umask() 方法中传递一个八进制值作为参数
# Python program to explain os.umask() method
# importing os module
import os
# Octal value for umask
# octal value 0o777 is
# 511 in decimal
mask = 0o777
# Set the current umask value
# and get the previous
# umask value
umask = os.umask(mask)
# Print the
# current and previous
# umask value
print("Current umask:", mask)
print("Previous umask:", umask)
输出:
Current umask: 511
Previous umask: 18