| by Arround The Web | No comments

Python chmod

When working with the files and directories in Python, file permissions play a crucial role in controlling access to files and directories. In such an instance, the Python “os.chmod()” of the os module allows users to change file permissions through Python programming.

This guide will explore the basics of file permissions, explain how to use Python “chmod” to modify the file permissions, and provide some practical examples. This article discusses the following topics:

    • What is the “os.chmod()” Function in Python?
    • Setting Permissions For Files.
    • Change File Ownership Using os.chmod() Function.
    • Python chmod with Recursive Flag.

What is the “os.chmod()” Function in Python?

The “os.chmod()” function in Python is used to change the permissions of a particular file or directory. It allows the users to set the read, write, and execute permissions for the owner, group, and others.

Syntax

os.chmod(path, mode)

 
According to the above syntax:

    • The “path” parameter is the path to the file or directory whose permissions need to be changed. Strings and byte objects are both acceptable.
    • The “mode” parameter is an integer that represents the new permissions for the specified file or directory.
    • The permissions are denoted as a combination of the following constants:
      • stat.S_ISUID: When the script is executed, it sets the user ID.
      • stat.S_ISGID: Sets the group ID during execution.
      • stat.S_ENFMT: Record locking enforced and others.

    Example 1: Setting Permissions For Files

    Before going to the code, let’s have a look at the original path of the file:


    Code

    The following code is used to set the permission for files:

    import os
    os.chmod(r'C:\Users\p\Documents\program\sample.txt', 0o777)
    print('file can be read, write and execute for owner, group and others')

    os.chmod(r'C:\Users\p\Documents\program\sample.txt', 0o400)
    print('file can be read only for owner')

    os.chmod(r'C:\Users\p\Documents\program\sample.txt', 0o600)
    print('file can be read and write only for owner')

     
    In the above code block, perform the following steps:

      • The module named “os” is imported at the start of the program.
      • The “os.chmod()” function is used multiple times in the above program to accept the path and specified mode to change the permission of the given file.
      • The mode “0o777” is used to change the permission of the file to “777” which indicates that it can be read, written, and executed for the owner, group, and others.
      • Similarly, the mode “0o400” changes the permission of the file to “400” which means that it can be read only by the owner.
      • Lastly, the “0o600” mode is used to change the permission of the file to “600” which means that it can be read and written only for the owner.

     
    Output


    As seen, the file mode has been changed successfully.

    Example 2: Change File Ownership Using “os.chmod()” Function Parameters Value

    The following code is used to change file ownership using the “os.chmod()” function parameters value:

    import os, sys, stat
    os.chmod(r"C:\Users\p\Documents\program\sample.txt", stat.S_IWRITE)
    os.chmod(r"C:\Users\p\Documents\program\sample.txt", stat.S_IXUSR)
    print("File can be written and executed only by owner.")

     
    In the above code:

      • The modules named “os”, “sys” and “stat” are imported.
      • The “os.chmod()” function takes the specified mode such as “stat.S_IWRITE”, and “stat.S_IXUSR” and file path as an argument to change the file ownership.

    Output


    This output signifies that the file permission has been changed successfully.

    Conclusion

    The “os.chmod()” function of the “os” module is used to change the ownership of the Python file by accepting the path and mode as an argument. The different numerical notation and specified descriptors are used as mode parameters of the “os.chmod()” function. This guide presented an in-depth guide on the Python “os.chmod()” function of the os module.

Share Button

Source: linuxhint.com

Leave a Reply