
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to change the owner of a file using Python?
You can change the owner of a file or a directory using the pwd, grp and os modules. The uid module is used to get the uid from user name, grp to get gid group name string and os to change the owner:
Example
import pwd import grp import os uid = pwd.getpwnam("nobody").pw_uid gid = grp.getgrnam("nogroup").gr_gid path = 'my_folder' os.chown(path, uid, gid)
- Related Articles
- How to change the owner of a directory using Python?
- How to change the file owner and group in Linux?
- How to change the permission of a file using Python?
- How to change the mode of a file using Python?
- How to change file permissions in Python?
- How to change file extension in Python?
- How to check the permissions of a file using Python?
- How to change the permission of a directory using Python?
- How to create a duplicate file of an existing file using Python?
- How to get stat of a file using Python?
- How to create hardlink of a file using Python?
- How to create softlink of a file using Python?
- How to rename a file using Python?
- How to remove a file using Python?
- How to find a file using Python?

Advertisements