Determining Whether a Directory is Writeable

前端 未结 10 1911
萌比男神i
萌比男神i 2020-12-07 14:44

What would be the best way in Python to determine whether a directory is writeable for the user executing the script? Since this will likely involve using the os module I sh

10条回答
  •  感情败类
    2020-12-07 15:13

    Check the mode bits:

    def isWritable(name):
      uid = os.geteuid()
      gid = os.getegid()
      s = os.stat(dirname)
      mode = s[stat.ST_MODE]
      return (
         ((s[stat.ST_UID] == uid) and (mode & stat.S_IWUSR)) or
         ((s[stat.ST_GID] == gid) and (mode & stat.S_IWGRP)) or
         (mode & stat.S_IWOTH)
         )
    

提交回复
热议问题