Python os.chown方法

Python os.chown(path, uid, gid) 方法用于将path的所有者和group修改为指定的uidgid

  • 如果不修改可以将uid或gid设置为 -1
  • 需要超级用户权限来执行修改操作
  • os.chown()方法只在UNIX/LINUX平台上可用

os.chown 语法

os.chown(path, uid, gid, *, dir_fd = None, follow_symlinks = True)

os.chown 参数

  • path - 设置权限的文件路径
  • uid - 所属用户 ID
  • gid - 所属用户组 ID
  • dir_fd (可选) - 指向一个目录的FD
  • follow_symlinks (可选) - Bool值,默认是True。当为False时,该方法将对符号链接本身而不是链接指向的文件进行操作。

参数列表中的*表示所有后面的参数都是关键字参数(在我们例子中是dir_fdfollow_symlinks),也就是非位置参数。

os.chown 返回值

os.chown方法没有返回值。

os.chown 示例1: 简单用法

# Api demos for os.chown() method
import os

path = "apidemos.txt"

# Print the UID and GID of the file
print("UID of the file:", os.stat(path).st_uid)
print("GID of the file:", os.stat(path).st_gid)

os.chown(path, 4000, 4000)
print("\nUID and GID of this file has changed")

# Print the UID and GID of the file again
print("\nOwner id of the file:", os.stat(path).st_uid)
print("Group id of the file:", os.stat(path).st_gid)

输出:

Python os.chown

os.chown 示例2: 只修改UID

# Api demos for os.chown() method
import os

path = "apidemos.txt"

# Print the UID and GID of the file
print("UID of the file:", os.stat(path).st_uid)
print("GID of the file:", os.stat(path).st_gid)

os.chown(path, 5000, -1)
print("\nUID of this file has changed")

# Print the UID and GID of the file again
print("\nOwner id of the file:", os.stat(path).st_uid)
print("Group id of the file:", os.stat(path).st_gid)

输出:

Python os.chown

os.chown 示例3: 修改符号链接文件本身的UID和GID

# Api demos for os.chown() method
import os

path = "apidemos.txt"

# using os.symlink() method to create a symlink for path  
path_symlink = "apidemos_symlink.txt"
os.symlink(path, path_symlink)

# Print the UID and GID of the path and path_symlink
print("UID of the path:", os.stat(path).st_uid)
print("GID of the path:", os.stat(path).st_gid)
print("UID of the path_symlink:", os.stat(path_symlink).st_uid)
print("GID of the path_symlink:", os.stat(path_symlink).st_gid)

# Case 1: change the path_symlink file's UID and GID with default follow_symlinks
os.chown(path_symlink, 4000, 4000)
# Print the UID and GID of the path and path_symlink
print("Case 1: change the path_symlink file's UID and GID with default follow_symlinks to 1000")
print("UID of the path:", os.stat(path).st_uid)
print("GID of the path:", os.stat(path).st_gid)
print("UID of the path_symlink:", os.stat(path_symlink).st_uid)
print("GID of the path_symlink:", os.stat(path_symlink).st_gid)

# Case 2: change the path_symlink file's UID and GID with follow_symlinks=False
os.chown(path_symlink, 3000, 3000, follow_symlinks = False)
# Print the UID and GID of the path and path_symlink
print("Case 2: change the path_symlink file's UID and GID with follow_symlinks=False to 2000")
print("UID of the path:", os.stat(path).st_uid)
print("GID of the path:", os.stat(path).st_gid)
print("UID of the path_symlink:", os.stat(path_symlink).st_uid)
print("GID of the path_symlink:", os.stat(path_symlink).st_gid)

输出:

Python os.chown

我们可以看到,当设置follow_symlinks = False时,执行os.chown只修改的符号链接文件本身的UID和GID,被链接的文件的UID和GID未发生变化。

赞(1)
未经允许不得转载:极客笔记 » Python os.chown方法

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
Python OS模块
Python os.chown方法Python os.write() 方法Python os.pardir 方法
Python String模块
Python String capitalize()方法Python String count()方法Python String center()方法Python String expandtabs()方法Python String index()方法Python String isalnum()方法Python String endswith()方法Python String encode()方法Python String find() 方法Python String decode()方法Python String isalpha() 方法Python String isdigit() 方法Python String islower() 方法Python String isnumeric() 方法Python String isspace() 方法Python String istitle() 方法Python String isupper() 方法Python String join() 方法Python String len() 方法Python String ljust() 方法Python String lower() 方法Python String lstrip() 方法Python String maketrans() 方法Python String max() 方法Python String min() 方法Python String replace() 方法Python String rfind() 方法Python String rindex() 方法Python String rjust() 方法Python String rstrip() 方法Python String isdecimal() 方法Python String splitlines() 方法Python String split() 方法Python String startswith() 方法Python String swapcase() 方法Python String strip() 方法Python String translate() 方法Python String title() 方法Python String zfill() 方法Python String upper() 方法
Python Math 模块
Python Math exp() 函数Python Math ceil() 函数Python Math floor() 函数Python Math fabs() 函数Python Math log10() 函数Python Math log() 函数Python Math pow() 函数Python Math modf() 函数Python round() 函数Python Math sqrt() 函数Python Math acos() 函数Python Math asin() 函数Python Math atan() 函数Python Math atan2() 函数Python Math cos() 函数Python Math degrees() 函数Python Math hypot() 函数Python Math radians() 函数Python Math sin() 函数Python Math tan() 函数
Python Random 模块
Python random choice() 函数Python random random() 函数Python random randrange() 函数Python random seed() 函数Python random shuffle() 函数Python random uniform() 函数
Python List 模块
Python List min() 方法Python List len() 方法Python List list() 方法Python List max() 方法