check permissions of directories in python

i want a python program that given a directory, it will return all directories within that directory that have 775 (rwxrwxr-x) permissions


Asked by: Emily344 | Posted: 06-12-2021

Answer 1

Neither answer recurses, though it's not entirely clear that that's what the OP wants. Here's a recursive approach (untested, but you get the idea):

import os
import stat
import sys

MODE = "775"

def mode_matches(mode, file):
    """Return True if 'file' matches 'mode'.

    'mode' should be an integer representing an octal mode (eg
    int("755", 8) -> 493).
    # Extract the permissions bits from the file's (or
    # directory's) stat info.
    filemode = stat.S_IMODE(os.stat(file).st_mode)

    return filemode == mode

    top = sys.argv[1]
except IndexError:
    top = '.'

    mode = int(sys.argv[2], 8)
except IndexError:
    mode = MODE

# Convert mode to octal.
mode = int(mode, 8)

for dirpath, dirnames, filenames in os.walk(top):
    dirs = [os.path.join(dirpath, x) for x in dirnames]
    for dirname in dirs:
        if mode_matches(mode, dirname):
            print dirname

Something similar is described in the stdlib documentation for stat.

Answered by: Sawyer573 | Posted: 07-01-2022

Answer 2

Take a look at the os module. In particular os.stat to look at the permission bits.

import  os

for filename in os.listdir(dirname):
   path=os.path.join(dirname, filename)
   if os.path.isdir(path):
       if (os.stat(path).st_mode & 0777) == 0775:
           print path

Answered by: John737 | Posted: 07-01-2022

Answer 3

Compact generator based on Brian's answer:

import os

(fpath for fpath 
   in (os.path.join(dirname,fname) for fname in os.listdir(dirname)) 
   if (os.path.isdir(fpath) and (os.stat(fpath).st_mode & 0777) == 0775))

Answered by: Marcus613 | Posted: 07-01-2022

Answer 4

Does it have to be python?

You can also use find to do that :

"find . -perm 775"

Answered by: Elise873 | Posted: 07-01-2022

Answer 5

You can check 775 permission for files and directories using below command

path="location of file or directory"
if (oct(os.stat(path).st_mode)[-3:]=="775"):

Answered by: Elise299 | Posted: 07-01-2022

Similar questions

python - Do I have to change directories and give permissions everytime I try to run a file directly from Terminal?

I wrote some simple code that would would open google maps and would search for the address that you pass to it as an attribute.The name of the file is The code is: #! /usr/bin/env python3 #! python3 import sys,webbrowser a=sys.argv b=a[1:] main=' '.join(b)'' +str(main)) As you can see, this is designed to run...

zip - Set permissions on a compressed file in python

I have a file test.txt that is inside a zip archive The permissions on test.txt are out of my control when it's compressed, but now I want them to be group-writeable. I am extracting the file with Python, and don't want to escape out to the shell. EDIT: Here's what I've got so far: import zipfile z = zipfile.ZipFile('', 'w'...

python - Will Django be a good choice for a permissions based web-app?

I've been exploring the details of Django for about a week now and like what I see. However I've come upon some.. negativity in relation to fine-grained control of permissions to the CRUD interface. What I'm writing is an Intranet client management web-app. The organisation is about 6 tiers, and I need to restrict access to client groups based on tiers. Continually expanding. I have a fairly good idea how I'm going...

python - Amazon S3 permissions

Trying to understand S3...How do you limit access to a file you upload to S3? For example, from a web application, each user has files they can upload, but how do you limit access so only that user has access to that file? It seems like the query string authentication requires an expiration date and that won't work for me, is there another way to do this?

python - Permissions for a site only

I have a multilingual Django project. Every language is a different subdomain. So we've decided to use the "sites" application and to create one different site for every language. On that project, I also have a "pages" application, which is quite similar to a CMS. The user can create pages with content and they'll be displayed in the appropriate language site. Now I'm looking to be able to manage advanced p...

python - DB Permissions with Django unit testing

Disclaimer: I'm very new to Django. I must say that so far I really like it. :) (now for the "but"...) But, there seems to be something I'm missing related to unit testing. I'm working on a new project with an Oracle backend. When you run the unit tests, it immediately gives a permissions error when trying to create the schema. So, I get what it's trying to do (create a clean sandbox), but what...

python - How do I change permissions to a socket?

I am trying to run a simple Python based web server given here. And I get the following error message: Traceback (most recent call last): File "", line 63, in <module> main() File "", line 55, in main server = HTTPServer(('', 80), MyHandler) File "/usr/lib/python2.5/SocketSe...

linux - Dropping Root Permissions In Python

I'd like to have a Python program start listening on port 80, but after that execute without root permissions. Is there a way to drop root or to get port 80 without it?

setting permissions of python module (python setup install)

I am configuring a distutils-based for a python module that is to be installed on a heterogeneous set of resources. Due to the heterogeneity, the location where the module is installed is not the same on each host however disutils picks the host-specific location. I find that the module is installed without o+rx permissions using disutils (in spite of setting umask ahead of running One solutio...

python - Django 1.2 object level permissions - third party solutions?

python - Django Inlines user permissions + view only - permissions issues

I'm not sure if this is a bug or I'm just missing something (although I have already parsed the documentation about inlines), but: Let's say I have a model A. Model A is an inline of model B. User U has full access to model B, but only change permissions to model A (so, no add, nor delete). However, when editing model B, user U can still see the "Add another A" link at the bottom, although U hasn't add perm...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python