Friday, December 27, 2024
Google search engine
HomeLanguagesPython | os.umask() method

Python | os.umask() method

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

os.umask() method in Python is used to set the current numeric umask value and get the previous umask value.

umask stands for user file-creation mode mask. This is used to determine the file permission for newly created files or directories.

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.

Code #1: Use of os.umask() method




# 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) 


Output:

Current umask: 18
Previous umask: 54

 

Code #2: Passing an octal value as parameter in os.umask() method




# 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) 


Output:

Current umask: 511
Previous umask: 18
RELATED ARTICLES

Most Popular

Recent Comments