how to find the owner of a file or directory in python

前端 未结 7 1563
青春惊慌失措
青春惊慌失措 2020-12-03 04:38

I need a function or method in Python to find the owner of a file or directory.

The function should be like:

>>> find_owner(\"/home/somedir         


        
相关标签:
7条回答
  • 2020-12-03 04:56

    Here is some example code, showing how you can find the owner of file:

    #!/usr/bin/env python
    import os
    import pwd
    filename = '/etc/passwd'
    st = os.stat(filename)
    uid = st.st_uid
    print(uid)
    # output: 0
    userinfo = pwd.getpwuid(st.st_uid)
    print(userinfo)
    # output: pwd.struct_passwd(pw_name='root', pw_passwd='x', pw_uid=0, 
    #          pw_gid=0, pw_gecos='root', pw_dir='/root', pw_shell='/bin/bash')
    ownername = pwd.getpwuid(st.st_uid).pw_name
    print(ownername)
    # output: root
    
    0 讨论(0)
  • 2020-12-03 04:58

    It's an old question, but for those who are looking for a simpler solution with Python 3.

    You can also use Path from pathlib to solve this problem, by calling the Path's owner and group method like this:

    from pathlib import Path
    
    path = Path("/path/to/your/file")
    owner = path.owner()
    group = path.group()
    print(f"{path.name} is owned by {owner}:{group}")
    

    So in this case, the method could be the following:

    from typing import Union
    from pathlib import Path
    
    def find_owner(path: Union[str, Path]) -> str:
        path = Path(path)
        return f"{path.owner()}:{path.group()}"
    
    0 讨论(0)
  • 2020-12-03 05:07

    I'm not really much of a python guy, but I was able to whip this up:

    from os import stat
    from pwd import getpwuid
    
    def find_owner(filename):
        return getpwuid(stat(filename).st_uid).pw_name
    
    0 讨论(0)
  • 2020-12-03 05:13

    See os.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.getpwuid.

    0 讨论(0)
  • 2020-12-03 05:14

    You want to use os.stat():

    os.stat(path)
     Perform the equivalent of a stat() system call on the given path. 
     (This function follows symlinks; to stat a symlink use lstat().)
    
    The return value is an object whose attributes correspond to the 
    members of the stat structure, namely:
    
    - st_mode - protection bits,
    - st_ino - inode number,
    - st_dev - device,
    - st_nlink - number of hard links,
    - st_uid - user id of owner,
    - st_gid - group id of owner,
    - st_size - size of file, in bytes,
    - st_atime - time of most recent access,
    - st_mtime - time of most recent content modification,
    - st_ctime - platform dependent; time of most recent metadata 
                 change on Unix, or the time of creation on Windows)
    

    Example of usage to get owner UID:

    from os import stat
    stat(my_filename).st_uid
    

    Note, however, that stat returns user id number (for example, 0 for root), not actual user name.

    0 讨论(0)
  • 2020-12-03 05:19

    I stumbled across this recently, looking to get owner user and group information, so I thought I'd share what I came up with:

    import os
    from pwd import getpwuid
    from grp import getgrgid
    
    def get_file_ownership(filename):
        return (
            getpwuid(os.stat(filename).st_uid).pw_name,
            getgrgid(os.stat(filename).st_gid).gr_name
        )
    
    0 讨论(0)
提交回复
热议问题