YouTip LogoYouTip

Python3 Os Chmod

# Python3.x Python3 os.chmod() Method [![Image 3: Python3 OS File/Directory Methods](#) Python3 OS File/Directory Methods](#) * * * ### Overview The os.chmod() method is used to change the permissions of a file or directory. Available on Unix systems. ### Syntax The syntax for the **chmod()** method is as follows: os.chmod(path, mode) ### Parameters * **path** -- The file path or directory path. * **flags** -- Can be generated using bitwise OR operations with the following options. Read permission for a directory means you can get a list of filenames in the directory. Execute permission means you can change the working directory to this directory. To delete or add files in a directory, you must have both write and execute permissions. File permissions are checked in the order of user ID -> group ID -> others, and the first matching allow or deny permission is applied. * **stat.S_IXOTH:** Others have execute permission 0o001 * **stat.S_IWOTH:** Others have write permission 0o002 * **stat.S_IROTH:** Others have read permission 0o004 * **stat.S_IRWXO:** Others have all permissions (permission mask) 0o007 * **stat.S_IXGRP:** Group users have execute permission 0o010 * **stat.S_IWGRP:** Group users have write permission 0o020 * **stat.S_IRGRP:** Group users have read permission 0o040 * **stat.S_IRWXG:** Group users have all permissions (permission mask) 0o070 * **stat.S_IXUSR:** Owner has execute permission 0o100 * **stat.S_IWUSR:** Owner has write permission 0o200 * **stat.S_IRUSR:** Owner has read permission 0o400 * **stat.S_IRWXU:** Owner has all permissions (permission mask) 0o700 * **stat.S_ISVTX:** In a directory, only the owner can delete or change files 0o1000 * **stat.S_ISGID:** When executing this file, the process's effective group is the file's group 0o2000 * **stat.S_ISUID:** When executing this file, the process's effective user is the file's owner 0o4000 * **stat.S_IREAD:** Set to read-only on Windows * **stat.S_IWRITE:** Remove read-only on Windows ### Return Value This method does not return a value. ### Example The following example demonstrates the use of the chmod() method: #!/usr/bin/python3import os, sys, stat # Assume the /tmp/foo.txt file exists, set the file to be executable by the user group os.chmod("/tmp/foo.txt", stat.S_IXGRP)# Set the file to be writable by other users os.chmod("/tmp/foo.txt", stat.S_IWOTH)print ("Modification successful!!") Executing the above program produces the following output: Modification successful!! [![Image 4: Python3 OS File/Directory Methods](#) Python3 OS File/Directory Methods](#)
← Java Hashmap PutifabsentJava Hashmap Put β†’