- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
How to change file permissions in Python?
To change the permission of a file, you can use the os.chmod(file, mode) call. Note that the mode should be specified in octal representation and therefore must begin with a 0o. For example, to make a file readonly, you can set the permission to 0o777, you can use:
>>> import os >>> os.chmod('my_file', 0o777)
You can also use flags from the stat module. You can read more about these flags here: http://docs.python.org/2/library/stat.html
Another way to acheive it is using a subprocess call:
>>> import subprocess >>> subprocess.call(['chmod', '0444', 'my_file'])
- Related Articles
- How to set file permissions in Java?
- How to check the permissions of a file using Python?
- File Permissions in C#
- File Permissions in java
- How to Copy File Permissions and Ownership to Another File in Linux?
- How to change file extension in Python?
- How to change the user and group permissions for a directory using Python?
- How to change the permission of a file using Python?
- How to change the mode of a file using Python?
- How to change the owner of a file using Python?
- How to check the permissions of a directory using Python?
- Getting root permissions on a file inside of vi on Linux
- How to Copy NTFS permissions using PowerShell?
- How to change file or directory permission in Linux/Unix?
- How to change the file owner and group in Linux?