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 use 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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Current umask: 18
Previous umask: 54

 

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Current umask: 511
Previous umask: 18


My Personal Notes arrow_drop_up

self motivated and passionate programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.