Python os.chown() Method

Python OS File/Directory Methods


Overview

os.chown() method is used to change the file owner. If you do not want to modify it, you can set it to -1. You need superuser privileges to perform permission modification operations.

Only supported on Unix.

Syntax

chown()The method syntax is as follows:

os.chown(path, uid, gid);

Parameters

  • path-- File path for setting permissions

  • uid-- User ID

  • gid-- Group ID

Return Value

This method has no return value.

Examples

The following example demonstrates the use of the chown() method:

Example

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# Assume the file /tmp/foo.txt exists.
# Set owner ID to 100
os.chown("/tmp/foo.txt", 100, -1)

print "Permission modified successfully!!"

The output of the above program is:

Permission modified successfully!!

Python OS File/Directory Methods

Other Extensions