Discuz! BBS

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 775|回复: 0

获取对象所有者,所有组,访问权限

[复制链接]

254

主题

363

帖子

2435

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2435
发表于 2023-7-11 18:37:14 | 显示全部楼层 |阅读模式
  1. import os, sys
  2. from stat import *
  3. def walktree(top, callback):
  4.     for f in os.listdir(top):
  5.         pathname = os.path.join(top. f)
  6.         mode = os.lstat(pathname).st_mode
  7.         if S_ISDIR(mode):
  8.             walktree(pathname, callback)
  9.         elif S_ISREG(mode):
  10.             # is a file, call the callback function
  11.             callback(pathname)
  12.         else:
  13.             # Unknown file type, print a message
  14.             print('Skipping %s' % pathname)

  15. def visitfile(file):
  16.     print('visiting', file)

  17. if __name__ == '__main__':
  18.         walktree(sys.argv[1], visitfile)
复制代码



其二:
  1. import os
  2. import pwd
  3. import grp
  4. from stat import *

  5. # input: full path of an Entry, for user and group and access privilige to get
  6. # output: (user,group,listofprivilige) of that Entry
  7. def get_user_attr(path):
  8.     stat = os.stat(path)
  9.     mode = os.stat(path).st_mode
  10.     uid = stat.st_uid
  11.     gid = stat.st_gid

  12.     is_readable_u = 'r' if (mode & S_IRUSR > 0) else '-'
  13.     is_readable_g = 'r' if (mode & S_IRGRP > 0) else '-'
  14.     is_readable_o = 'r' if (mode & S_IROTH > 0) else '-'

  15.     is_writable_u = 'w' if (mode & S_IWUSR > 0) else '-'
  16.     is_writable_g = 'w' if (mode & S_IWGRP > 0) else '-'
  17.     is_writable_o = 'w' if (mode & S_IWOTH > 0) else '-'

  18.     is_execable_u = 'x' if (mode & S_IXUSR > 0) else '-'
  19.     is_execable_g = 'x' if (mode & S_IXGRP > 0) else '-'
  20.     is_execable_o = 'x' if (mode & S_IXOTH > 0) else '-'

  21.     user_info_ = pwd.getpwuid(uid)
  22.     user = user_info_.pw_name

  23.     group_info_ = grp.getgrgid(gid)
  24.     group = group_info_.gr_name

  25.     ls_access = []
  26.     ls_access.append(is_readable_u)
  27.     ls_access.append(is_writable_u)
  28.     ls_access.append(is_execable_u)
  29.     ls_access.append(is_readable_g)
  30.     ls_access.append(is_writable_g)
  31.     ls_access.append(is_execable_g)
  32.     ls_access.append(is_readable_o)
  33.     ls_access.append(is_writable_o)
  34.     ls_access.append(is_execable_o)

  35.     return (user, group, ls_access)

  36. # input: full path of an Entry, may represents a file, a dir ,a link, and a ?
  37. # output: line of the path Entry's attribute, with '\n', for write to output file.
  38. def get_file_attr(path):

  39.     retStr = "null"
  40.     mode = os.stat(path).st_mode
  41.     if S_ISDIR(mode):
  42.         retStr = '_dir  ' + path + '\n'
  43.     elif S_ISREG(mode):
  44.         retStr = '_file ' + path + '\n'
  45.     elif S_ISLNK(mode):
  46.         retStr = '_lnk  ' + path + '\n'
  47.     else:
  48.         retStr = '?___  ' + path + '\n'
  49.     return retStr
复制代码



以上两法可以获取linux的文件权限、所有者、对象属性。

另附一文:Python速查手册,编程必用。
PythonQuickReference.pdf (1008.27 KB, 下载次数: 1)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-4-16 10:39 , Processed in 0.014888 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表