2009-05-29 47 views

回答

28

使用os.stat()來獲取文件的uid和gid。然後,分別使用pwd.getpwuid()grp.getgrgid()來獲取用戶名和組名。

import grp 
import pwd 
import os 

stat_info = os.stat('/path') 
uid = stat_info.st_uid 
gid = stat_info.st_gid 
print uid, gid 

user = pwd.getpwuid(uid)[0] 
group = grp.getgrgid(gid)[0] 
print user, group 
0

我傾向於使用os.stat

給定的路徑上執行統計系統調用。返回值是屬性對應於stat結構成員的對象,即:st_mode(保護位),st_ino(inode編號),st_dev(設備),st_nlink(硬鏈接數),st_uid(用戶標識所有者),st_gid(所有者的組ID),st_size(文件的大小,以字節爲單位),st_atime(最近訪問的時間),st_mtime(最近內容修改的時間),st_ctime(平臺相關的;最近的時間Unix上的元數據更改或Windows上的創建時間)

在上面的鏈接os.stat上有一個示例。

0

使用os.stat函數。

0

使用os.stat

>>> s = os.stat('.') 
>>> s.st_uid 
1000 
>>> s.st_gid 
1000 

st_uid是所有者的用戶ID,st_gid是組ID。請參閱鏈接文檔以獲取可通過stat激活的其他信息。

2

因爲Python 3.4.4,所述Pathpathlib模塊爲此提供了一個很好的語法:

from pathlib import Path 
whatever = Path("relative/or/absolute/path/to_whatever") 
if whatever.exists(): 
    print("Owner: %s" % whatever.owner()) 
    print("Group: %s" % whatever.group()) 
相關問題