How can I get the owner and group IDs of a directory using Python under 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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With