Python os.chmod() Method
- Syntax of the os.chmod() Method
- Example Codes: Working With the os.chmod() Method in Python
- Example Codes: Set Permission to an Octal Number Using the os.chmod() Method in Python
Python os.chmod() method is an efficient way of changing the permission of a file/directory to the specified mode entered as a parameter.
Syntax of the os.chmod() Method
Parameters
Return
In the execution process, this method does not return any value.
Example Codes: Working With the os.chmod() Method in Python
import os, sys, stat os.chmod("/home/file.txt", stat.S_IREAD) print("Now, this file can be read only by the owner.") os.chmod("/home/file.txt", stat.S_IRGRP) print("Now, this file can be read only by a group of users.") os.chmod("/home/file.txt", stat.S_IROTH) print("The file access has changed; now it can be read by others.")
Now, this file can be read only by the owner. Now, this file can be read only by a group of users. The file access has changed; now it can be read by others.
This method can work on a specified file descriptor, an absolute or a relative path, and it might work on a symlink on some systems.
Example Codes: Set Permission to an Octal Number Using the os.chmod() Method in Python
import os file_name = "file.txt" os.chmod(file_name, 0o777) stat = os.stat(file_name) mode = oct(stat.st_mode)[-3:] print(mode)
When using the os.chmod() method, write 0o before the permissions to set because they represent an octal integer. In the above code, the file’s permission is set to 777 , meaning anyone can do anything they want with the file.
Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.
Related Article — Python OS
Python права на выполнение
- Python | os.ctermid() method
- Python | os.environ object
- Python os.chdir() method
- Python | os.fchdir() method
- Python | os.getcwd() method
- Python | os.getenv() method
- Python | os.get_exec_path() method
- Python | os.geteuid() and seteuid() method
- Python | os.getgrouplist() method
- Python | os.getgroups() method
- Python | os.getlogin() method
- Python | os.getpgid() method
- Python | os.getpgrp() method
- Python | os.getpid() method
- Python | os.getppid() method
- Python | os.getresuid() and os.setresuid() method
- Python | os.getuid() and os.setuid() method
- Python | os.setregid() method
- Python | os.setreuid() method
- Python | os.setgroups() method
- Python | os.getsid() method
- Python | os.strerror() method
- Python | os.supports_bytes_environ object
- Python | os.umask() method
- Python | os.link() method
- Python | os.listdir() method
- Python | os.mkdir() method
- Python | os.makedirs() method
- Python | os.mkfifo() method
- Python | os.major() method
- Python | os.minor() method
- Python | os.makedev() method
- Python | os.readlink() method
- Python | os.remove() method
- Python | os.removedirs() method
- Python | os.rename() method
- Python | os.renames() method
- Python – os.replace() method
- Python | os.rmdir() method
- Python | os.scandir() method
- Python | os.stat() method
- Python | os.statvfs() method
- Python | os.sync() method
- Python | os.truncate() method
- Python | os.unlink() method
- os.walk() in Python
- Python | os.chmod method
- Python | os.abort() method
- Python | os._exit() method
- Python | os.fork() method
- Python | os.kill() method
- Python | os.nice() method
- Python | os.system() method
- Python | os.times() method
- Python | os.wait() method
- Python | os.chmod method
- Python | os.open() method
- Python | os.get_blocking() method
- Python | os.isatty() method
- Python | os.openpty() method
- Python | os.pipe() method
- Python | os.pipe2() method
- Python | os.pread() method
- Python | os.write() method
- Python | os.pwrite() method
- Python | os.read() method
- Python | os.sendfile() method
- Python | os.set_blocking() method
- Python | os.ctermid() method
- Python | os.environ object
- Python os.chdir() method
- Python | os.fchdir() method
- Python | os.getcwd() method
- Python | os.getenv() method
- Python | os.get_exec_path() method
- Python | os.geteuid() and seteuid() method
- Python | os.getgrouplist() method
- Python | os.getgroups() method
- Python | os.getlogin() method
- Python | os.getpgid() method
- Python | os.getpgrp() method
- Python | os.getpid() method
- Python | os.getppid() method
- Python | os.getresuid() and os.setresuid() method
- Python | os.getuid() and os.setuid() method
- Python | os.setregid() method
- Python | os.setreuid() method
- Python | os.setgroups() method
- Python | os.getsid() method
- Python | os.strerror() method
- Python | os.supports_bytes_environ object
- Python | os.umask() method
- Python | os.link() method
- Python | os.listdir() method
- Python | os.mkdir() method
- Python | os.makedirs() method
- Python | os.mkfifo() method
- Python | os.major() method
- Python | os.minor() method
- Python | os.makedev() method
- Python | os.readlink() method
- Python | os.remove() method
- Python | os.removedirs() method
- Python | os.rename() method
- Python | os.renames() method
- Python – os.replace() method
- Python | os.rmdir() method
- Python | os.scandir() method
- Python | os.stat() method
- Python | os.statvfs() method
- Python | os.sync() method
- Python | os.truncate() method
- Python | os.unlink() method
- os.walk() in Python
- Python | os.chmod method
- Python | os.abort() method
- Python | os._exit() method
- Python | os.fork() method
- Python | os.kill() method
- Python | os.nice() method
- Python | os.system() method
- Python | os.times() method
- Python | os.wait() method
- Python | os.chmod method
- Python | os.open() method
- Python | os.get_blocking() method
- Python | os.isatty() method
- Python | os.openpty() method
- Python | os.pipe() method
- Python | os.pipe2() method
- Python | os.pread() method
- Python | os.write() method
- Python | os.pwrite() method
- Python | os.read() method
- Python | os.sendfile() method
- Python | os.set_blocking() method