Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 os.fchown() Method

Python3 os.fchown() Method

Python3 OS File/Directory Methods Python3 OS File/Directory Methods


Overview

The os.fchown() method changes the ownership of a file; this function changes the user ID and group ID of the file specified by the file descriptor fd.

Available on Unix.

Syntax

The **fchown()** method syntax is as follows:


os.fchown(fd, uid, gid)

Parameters

Return Value

This method has no return value.

Example

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

Example (Python 3.0+)

#!/usr/bin/python3

import

os

,

sys

,

stat

Open the file "/tmp/foo.txt"

fd

=

os

.

open

(

"

/tmp

"

,

os

.

O_RDONLY

)

Set the file's user ID to 100

os

.

fchown

(

fd

,

100

, -

1

)

Set the file's group ID to 50

os

.

fchown

(

fd

, -

1

,

50

)

print

(

"

Permissions changed successfully!!

"

)

Closing a File

os

.

close

(

fd

)

The output of the above program is:


修改权限成功!!

Python3 OS File/Directory Methods Python3 OS File/Directory Methods