Checking folder/file ntfs permissions using python

后端 未结 1 1371
迷失自我
迷失自我 2021-02-20 04:00

As the question title might suggest, I would very much like to know of the way to check the ntfs permissions of the given file or folder (hint: those are the ones you see in the

1条回答
  •  北恋
    北恋 (楼主)
    2021-02-20 04:25

    Unless you fancy rolling your own, win32security is the way to go. There's the beginnings of an example here:

    http://timgolden.me.uk/python/win32_how_do_i/get-the-owner-of-a-file.html

    If you want to live slightly dangerously (!) my in-progress winsys package is designed to do exactly what you're after. You can get an MSI of the dev version here:

    http://timgolden.me.uk/python/downloads/WinSys-0.4.win32-py2.6.msi

    or you can just checkout the svn trunk:

    svn co http://winsys.googlecode.com/svn/trunk winsys

    To do what you describe (guessing slightly at the exact requirements) you could do this:

    import codecs
    from winsys import fs
    
    base = "c:/temp"
    with codecs.open ("permissions.log", "wb", encoding="utf8") as log:
      for f in fs.flat (base):
      log.write ("\n" + f.filepath.relative_to (base) + "\n")
      for ace in f.security ().dacl:
        access_flags = fs.FILE_ACCESS.names_from_value (ace.access)
        log.write (u"  %s => %s\n" % (ace.trustee, ", ".join (access_flags)))
    

    TJG

    0 讨论(0)
提交回复
热议问题