Python3 os.fchown()方法

fchown()方法更改通过 df 给定数字UID和GID文件的所有者和组ID。如要退出ID时不变,可将其设置为-1。
注意:此方法是在 Python2.6 起可用的。

语法

以下是 fchown() 方法的语法:
os.fchown(fd, uid, gid)

参数

  • fd -- 这是其所有者ID和组ID需要进行设置的文件描述符

  • uid -- 这是要设置的文件所有者ID

  • gid -- 这是要设置文件的组ID。

返回值

此方法不返回任何值。只在类 Unix 操作系统上可用。

示例

下面的示例显示 fchown()方法的使用。
#!/usr/bin/python3

import os, sys, stat

# Now open a file "/tmp/foo.txt"
fd = os.open( "/tmp", os.O_RDONLY )

# Set the user Id to 100 for this file.
os.fchown( fd, 100, -1)

# Set the group Id to 50 for this file.
os.fchown( fd, -1, 50)

print ("Changed ownership successfully!!")

# Close opened file.
os.close( fd )
当我们运行上面的程序,它会产生以下结果:
Changed ownership successfully!!

上一篇: Python3文件方法 下一篇: Python3 os文件目录的方法