Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the owner and group of a folder with Python on a Linux machine?

Tags:

How can I get the owner and group IDs of a directory using Python under Linux?

like image 341
dan Avatar asked May 29 '09 20:05

dan


People also ask

How do I list the owner of a folder in Linux?

Find file owner with ls command in Linux The best Linux command to find file owner is using “ls -l” command. Open the terminal then type ls -l filename in the prompt. The 3rd column is the file owner. The ls command should be available on any Linux system.

How do you show the owner of a file in Python?

stat. It gives you st_uid which is the user ID of the owner. Then you have to convert it to the name. To do that, use pwd.


1 Answers

Use os.stat() to get the uid and gid of the file. Then, use pwd.getpwuid() and grp.getgrgid() to get the user and group names respectively.

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 
like image 135
Ayman Hourieh Avatar answered Oct 10 '22 17:10

Ayman Hourieh