

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change the permission 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 Questions & Answers
- How to change the permission of a directory using Python?
- How to change the mode of a file using Python?
- How to change the owner of a file using Python?
- How to change file or directory permission in Linux/Unix?
- How can I get a file's permission mask using Python?
- How to change SMB shared folder access permission using PowerShell?
- How to change the owner of a directory using Python?
- How to disable delete permission of File and Directory in Linux?
- How to check the permissions of a file using Python?
- How to change file permissions in Python?
- How to change file extension in Python?
- How to create hardlink of a file using Python?
- How to create softlink of a file using Python?
- How to get stat of a file using Python?
- How to create a duplicate file of an existing file using Python?
Advertisements