
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
How to check the permissions of a directory using Python?
You can use os.access(path, mode) to check the directory permission with modes for reading, writing and execution permissions. For being able to write you'll also need to check for execution permission. For example,
>>> import os >>> os.access('my_folder', os.R_OK) # Check for read access True >>> os.access('my_folder', os.W_OK) # Check for write access True >>> os.access('my_folder', os.X_OK) # Check for execution access True >>> os.access('my_folder', os.X_OK | ox.W_OK) # Check if we can write file to the directory True
You can also follow a common Python idiom: It's easier to ask for forgiveness than for permission. Following that idiom, you should try writing to the directory in question, and catch the error if you don't have the permission to do so.
- Related Articles
- How to check the permissions of a file using Python?
- How to change the user and group permissions for a directory using Python?
- How to change the permission of a directory using Python?
- How to change the owner of a directory using Python?
- How to check if a given directory contains any other directory in Python?
- How to create a directory using Python?
- How to remove a directory using Python?
- How to create a directory recursively using Python?
- How to remove a directory recursively using Python?
- How to calculate a directory size using Python?
- How to rename directory using Python?
- How to create a zip archive of a directory using Python?
- How to create a unique directory name using Python?
- How to Copy NTFS permissions using PowerShell?
- How to change current directory using Python?

Advertisements