HomeLinuxPython chmod

Python chmod


When working with the recordsdata and directories in Python, file permissions play a vital function in controlling entry to recordsdata and directories. In such an occasion, the Python “os.chmod()” of the os module permits customers to alter file permissions by means of Python programming.

This information will discover the fundamentals of file permissions, clarify tips on how to use Python “chmod” to switch the file permissions, and supply some sensible examples. This text discusses the next matters:

    • What’s the “os.chmod()” Perform in Python?
    • Setting Permissions For Information.
    • Change File Possession Utilizing os.chmod() Perform.
    • Python chmod with Recursive Flag.

What’s the “os.chmod()” Perform in Python?

The “os.chmod()” perform in Python is used to alter the permissions of a specific file or listing. It permits the customers to set the learn, write, and execute permissions for the proprietor, group, and others.

Syntax

 
In keeping with the above syntax:

    • The “path” parameter is the trail to the file or listing whose permissions must be modified. Strings and byte objects are each acceptable.
    • The “mode” parameter is an integer that represents the brand new permissions for the desired file or listing.
    • The permissions are denoted as a mixture of the next constants:
      • stat.S_ISUID: When the script is executed, it units the person ID.
      • stat.S_ISGID: Units the group ID throughout execution.
      • stat.S_ENFMT: Document locking enforced and others.

    Instance 1: Setting Permissions For Information

    Earlier than going to the code, let’s take a look on the unique path of the file:


    Code

    The next code is used to set the permission for recordsdata:

    import os
    os.chmod(r‘C:UserspDocumentsprogramsample.txt’, 0o777)
    print(‘file might be learn, write and execute for proprietor, group and others’)

    os.chmod(r‘C:UserspDocumentsprogramsample.txt’, 0o400)
    print(‘file might be learn just for proprietor’)

    os.chmod(r‘C:UserspDocumentsprogramsample.txt’, 0o600)
    print(‘file might be learn and write just for proprietor’)

     
    Within the above code block, carry out the next steps:

      • The module named “os” is imported firstly of this system.
      • The “os.chmod()” perform is used a number of occasions within the above program to just accept the trail and specified mode to alter the permission of the given file.
      • The mode “0o777” is used to alter the permission of the file to “777” which signifies that it may be learn, written, and executed for the proprietor, group, and others.
      • Equally, the mode “0o400” adjustments the permission of the file to “400” which signifies that it may be learn solely by the proprietor.
      • Lastly, the “0o600” mode is used to alter the permission of the file to “600” which signifies that it may be learn and written just for the proprietor.

     
    Output


    As seen, the file mode has been modified efficiently.

    Instance 2: Change File Possession Utilizing “os.chmod()” Perform Parameters Worth

    The next code is used to alter file possession utilizing the “os.chmod()” perform parameters worth:

    import os, sys, stat
    os.chmod(r“C:UserspDocumentsprogramsample.txt”, stat.S_IWRITE)
    os.chmod(r“C:UserspDocumentsprogramsample.txt”, stat.S_IXUSR)
    print(“File might be written and executed solely by proprietor.”)

     
    Within the above code:

      • The modules named “os”, “sys” and “stat” are imported.
      • The “os.chmod()” perform takes the desired mode akin to “stat.S_IWRITE”, and “stat.S_IXUSR” and file path as an argument to alter the file possession.

    Output


    This output signifies that the file permission has been modified efficiently.

    Conclusion

    The “os.chmod()” perform of the “os” module is used to alter the possession of the Python file by accepting the trail and mode as an argument. The totally different numerical notation and specified descriptors are used as mode parameters of the “os.chmod()” perform. This information introduced an in-depth information on the Python “os.chmod()” perform of the os module.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments