
- 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
How to change the mode of a file using 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 change the permission of a file using Python?
- How to change the owner of a file using Python?
- How to open a file in binary mode with Python?
- How to open a file in append mode with Python?
- How to open a binary file in append mode with Python?
- How to open a file in read and write mode with Python?
- How to change file permissions in Python?
- How to change file extension in Python?
- Python Program to open a file in the read-write mode without truncating the file
- How to check the permissions of a file using Python?
- Python Program to open a file in read-write mode with truncating file
- How to change the permission of a directory using Python?
- How to change the owner of a directory using Python?
- How to open a binary file in read and write mode with Python?
- How to create a duplicate file of an existing file using Python?

Advertisements