Easy Tutorial
❮ Python Os Major Python File Seek ❯

Python3 os.lchown() Method

Python3 OS File/Directory Methods


Overview

The os.lchown() method is used to change the owner of a file, similar to chown, but it does not follow symbolic links.

This method is only supported on Unix systems.

Syntax

The syntax for the lchown() method is as follows:

os.lchown(path, uid, gid)

Parameters

Return Value

This method does not return any value.

Example

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

#!/usr/bin/python3

import os, sys

# Open file
path = "/var/www/html/foo.txt"
fd = os.open( path, os.O_RDWR|os.O_CREAT )

# Close the opened file
os.close( fd )

# Change file permissions
# Set the owner user ID
os.lchown( path, 500, -1)

# Set the owner group ID
os.lchown( path, -1, 500)

print ("Permissions changed successfully!!")

Executing the above program will output:

Permissions changed successfully!!

Python3 OS File/Directory Methods

❮ Python Os Major Python File Seek ❯