- Python Basics
- Python - Home
- Python - Overview
- Python - History
- Python - Features
- Python vs C++
- Python - Hello World Program
- Python - Application Areas
- Python - Interpreter
- Python - Environment Setup
- Python - Virtual Environment
- Python - Basic Syntax
- Python - Variables
- Python - Data Types
- Python - Type Casting
- Python - Unicode System
- Python - Literals
- Python - Operators
- Python - Arithmetic Operators
- Python - Comparison Operators
- Python - Assignment Operators
- Python - Logical Operators
- Python - Bitwise Operators
- Python - Membership Operators
- Python - Identity Operators
- Python - Operator Precedence
- Python - Comments
- Python - User Input
- Python - Numbers
- Python - Booleans
- Python Control Statements
- Python - Control Flow
- Python - Decision Making
- Python - If Statement
- Python - If else
- Python - Nested If
- Python - Match-Case Statement
- Python - Loops
- Python - for Loops
- Python - for-else Loops
- Python - While Loops
- Python - break Statement
- Python - continue Statement
- Python - pass Statement
- Python - Nested Loops
- Python Functions & Modules
- Python - Functions
- Python - Default Arguments
- Python - Keyword Arguments
- Python - Keyword-Only Arguments
- Python - Positional Arguments
- Python - Positional-Only Arguments
- Python - Arbitrary Arguments
- Python - Variables Scope
- Python - Function Annotations
- Python - Modules
- Python - Built in Functions
- Python Strings
- Python - Strings
- Python - Slicing Strings
- Python - Modify Strings
- Python - String Concatenation
- Python - String Formatting
- Python - Escape Characters
- Python - String Methods
- Python - String Exercises
- Python Lists
- Python - Lists
- Python - Access List Items
- Python - Change List Items
- Python - Add List Items
- Python - Remove List Items
- Python - Loop Lists
- Python - List Comprehension
- Python - Sort Lists
- Python - Copy Lists
- Python - Join Lists
- Python - List Methods
- Python - List Exercises
- Python Tuples
- Python - Tuples
- Python - Access Tuple Items
- Python - Update Tuples
- Python - Unpack Tuples
- Python - Loop Tuples
- Python - Join Tuples
- Python - Tuple Methods
- Python - Tuple Exercises
- Python Sets
- Python - Sets
- Python - Access Set Items
- Python - Add Set Items
- Python - Remove Set Items
- Python - Loop Sets
- Python - Join Sets
- Python - Copy Sets
- Python - Set Operators
- Python - Set Methods
- Python - Set Exercises
- Python Dictionaries
- Python - Dictionaries
- Python - Access Dictionary Items
- Python - Change Dictionary Items
- Python - Add Dictionary Items
- Python - Remove Dictionary Items
- Python - Dictionary View Objects
- Python - Loop Dictionaries
- Python - Copy Dictionaries
- Python - Nested Dictionaries
- Python - Dictionary Methods
- Python - Dictionary Exercises
- Python Arrays
- Python - Arrays
- Python - Access Array Items
- Python - Add Array Items
- Python - Remove Array Items
- Python - Loop Arrays
- Python - Copy Arrays
- Python - Reverse Arrays
- Python - Sort Arrays
- Python - Join Arrays
- Python - Array Methods
- Python - Array Exercises
- Python File Handling
- Python - File Handling
- Python - Write to File
- Python - Read Files
- Python - Renaming and Deleting Files
- Python - Directories
- Python - File Methods
- Python - OS File/Directory Methods
- Object Oriented Programming
- Python - OOPs Concepts
- Python - Object & Classes
- Python - Class Attributes
- Python - Class Methods
- Python - Static Methods
- Python - Constructors
- Python - Access Modifiers
- Python - Inheritance
- Python - Polymorphism
- Python - Method Overriding
- Python - Method Overloading
- Python - Dynamic Binding
- Python - Dynamic Typing
- Python - Abstraction
- Python - Encapsulation
- Python - Interfaces
- Python - Packages
- Python - Inner Classes
- Python - Anonymous Class and Objects
- Python - Singleton Class
- Python - Wrapper Classes
- Python - Enums
- Python - Reflection
- Python Errors & Exceptions
- Python - Syntax Errors
- Python - Exceptions
- Python - try-except Block
- Python - try-finally Block
- Python - Raising Exceptions
- Python - Exception Chaining
- Python - Nested try Block
- Python - User-defined Exception
- Python - Logging
- Python - Assertions
- Python - Built-in Exceptions
- Python Multithreading
- Python - Multithreading
- Python - Thread Life Cycle
- Python - Creating a Thread
- Python - Starting a Thread
- Python - Joining Threads
- Python - Naming Thread
- Python - Thread Scheduling
- Python - Thread Pools
- Python - Main Thread
- Python - Thread Priority
- Python - Daemon Threads
- Python - Synchronizing Threads
- Python Synchronization
- Python - Inter-thread Communication
- Python - Thread Deadlock
- Python - Interrupting a Thread
- Python Networking
- Python - Networking
- Python - Socket Programming
- Python - URL Processing
- Python - Generics
- Python Libraries
- NumPy Tutorial
- Pandas Tutorial
- SciPy Tutorial
- Matplotlib Tutorial
- Django Tutorial
- OpenCV Tutorial
- Python Miscellenous
- Python - Date & Time
- Python - Maths
- Python - Iterators
- Python - Generators
- Python - Closures
- Python - Decorators
- Python - Recursion
- Python - Reg Expressions
- Python - PIP
- Python - Database Access
- Python - Weak References
- Python - Serialization
- Python - Templating
- Python - Output Formatting
- Python - Performance Measurement
- Python - Data Compression
- Python - CGI Programming
- Python - XML Processing
- Python - GUI Programming
- Python - Command-Line Arguments
- Python - Docstrings
- Python - JSON
- Python - Sending Email
- Python - Further Extensions
- Python - Tools/Utilities
- Python - GUIs
- Python Useful Resources
- Python Compiler
- NumPy Compiler
- Matplotlib Compiler
- SciPy Compiler
- Python - Programming Examples
- Python - Quick Guide
- Python - Useful Resources
- Python - Discussion
Python os.chown() Method
The Python os.chown() method changes the owner and group id of path to the specified uid and gid. To leave one of the ids unchanged, set the uid or gid to -1. To perform modification operations, you would require super user privilege.
For determining which system resources a user or group can access, Unix-like operating systems identify a user by a value called a user identifier (UID) and identify group by a group identifier (GID).
Note: This method is only available in UNIX/LINUX platforms.
Syntax
Following is the syntax of Python os.chown() method −
os.chown(path, uid, gid, *, dir_fd = None, follow_symlinks = True)
Parameters
path − This is the path for which owner id and group id needs to be setup.
uid − This is Owner ID to be set for the file.
gid − This is Group ID to be set for the file.
dir_fd − A file descriptor pointing to a directory. By default it is None.
follow_symlinks − By default it is True. When False, the method will operate on the symlink itself rather than the file to which the link points to.
Note: The * in the argument list means that all subsequent arguments are keyword arguments (dir_fd and follow_symlinks in our case), that is, non-positional arguments.
Return Value
This method does not return any value.
Example 1
If we does not want to change the gid, we can set it to -1.
The following example shows the usage of Python os.chown() method. Here the ownership of a file path is changed to the given uid '100' and gid '-1'. Hence, only the uid is modified.
import os, sys # Assuming /tmp/foo.txt exists. # To set owner ID 100 following has to be done. os.chown("/tmp/foo.txt", 100, -1) print ("Changed ownership successfully!!")
When we run above program, it produces following result −
Changed ownership successfully!!
Example 2
In here, we are trying to change both the uid and gid of the file path. The given uid is '3000' and gid is '2000'.
# importing the module import os filepath = "code.txt" # Printing the current UID and GID of the file print("The current UID of the file is:", os.stat(filepath).st_uid) print("The current GID of the file is:", os.stat(filepath).st_gid) # Using the method os.chown(path, 3000, 2000) print("\n The UID and GID of this file has been changed succesfully") # Printing the UID and GID of the file after modifying print("\n The modified uid of the file is:", os.stat(filepath).st_uid) print("The modified gid of the file is:", os.stat(filepath).st_gid)
While executing the above code we get the following output −
sarika@sarika-virtual-machine:~/Desktop$ sudo python3 chown.py The current UID of the file is: 1000 The current GID of the file is: 1000 The UID and GID of this file has been changed succesfully The modified uid of the file is: 3000 The modified gid of the file is: 2000
Example 2
By default the follow_symlink is True. When False, the method will operate on the symlink itself rather than the file to which the link points to.
In the example given below a symlink of the given file path "code.txt" is created. Then we are trying to change the uid and gid of the symlink pointing to the file path. Here, the default follow_symlink is used.
import os filepath = "code.txt" # creating a symlink for filepath symlink_path = "code_symlink.txt" os.symlink(filepath, symlink_path) # Printing the UID and GID of the filepath and symlink_path print("The UID of the filepath is:", os.stat(filepath).st_uid) print("The GID of the filepath is:", os.stat(filepath).st_gid) print("The UID of the symlink_path is:", os.stat(symlink_path).st_uid) print("The GID of the symlink_path is:", os.stat(symlink_path).st_gid) # changing the symlink_path UID and GID of the file with default follow_symlinks os.chown(symlink_path, 5000, 5000) # Printing the UID and GID of the filepath and symlink_path print("changing the symlink_path UID and GID of the file with default follow_symlinks") print("The UID of the filepath is:", os.stat(filepath).st_uid) print("The GID of the filepath is:", os.stat(filepath).st_gid) print("The UID of the symlink_path is:", os.stat(symlink_path).st_uid) print("The GID of the symlink_path is:", os.stat(symlink_path).st_gid)
Following is an output of the above code −
sarika@sarika-virtual-machine:~/Desktop$ sudo python3 chown.py The UID of the filepath is: 3000 The GID of the filepath is: 2000 The UID of the symlink_path is: 3000 The GID of the symlink_path is: 2000 changing the UID and GID of the symlink_path file with default follow_symlinks The UID of the filepath is: 5000 The GID of the filepath is: 5000 The UID of the symlink_path is: 5000 The GID of the symlink_path is: 5000
Example 3
In here, We can see that when follow_symlinks = False, the execution of os.chown() method only modifies the UID and GID of the symlink file itself. The UID and GID of the linked file have not changed.
import os filepath = "code.txt" # creating a symlink for filepath symlink_path = "code_symlink.txt" os.symlink(filepath, symlink_path) # Printing the UID and GID of the path and symlink_path print("The UID of the path is:", os.stat(filepath).st_uid) print("The GID of the path is:", os.stat(filepath).st_gid) print("The UID of the symlink_path is:", os.stat(symlink_path).st_uid) print("The GID of the symlink_path is:", os.stat(symlink_path).st_gid) # changing the UID and GID of the symlink_path file with follow_symlinks=False os.chown(symlink_path, 4000, 4000, follow_symlinks = False) # Printing the UID and GID of the filepath and symlink_path print("changing the symlink_path UID and GID of the file with follow_symlinks=False") print("The UID of the path is:", os.stat(filepath).st_uid) print("The GID of the path is:", os.stat(filepath).st_gid) print("The UID of the path_symlink is:", os.stat(symlink_path).st_uid) print("The GID of the path_symlink is:", os.stat(symlink_path).st_gid)
Output of the above code is as follows −
sarika@sarika-virtual-machine:~/Desktop$ sudo python3 chown.py The UID of the path is: 5000 The GID of the path is: 5000 The UID of the symlink_path is: 5000 The GID of the symlink_path is: 5000 changing the symlink_path UID and GID of the file with follow_symlinks=False The UID of the path is: 5000 The GID of the path is: 5000 The UID of the path_symlink is: 5000 The GID of the path_symlink is: 5000
To Continue Learning Please Login
Login with Google