Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing file permission in Python

I am trying to change permission of a file access:

os.chmod(path, mode) 

I want to make it read-only:

os.chmod(path, 0444) 

Is there any other way make a file read-only?

like image 905
Abul Hasnat Avatar asked Apr 27 '13 07:04

Abul Hasnat


People also ask

How do you change ownership of a file in Python?

chown() method in Python is used to change the owner and group id of the specified path to the specified numeric owner id (UID) and group id (GID). Note: os. chown() method is available only on UNIX platforms and the functionality of this method is typically available only to the superuser or a privileged user.

How do I change file permissions?

If you are not the owner of the file or directory, become superuser or assume an equivalent role. Only the current owner or superuser can use the chmod command to change file permissions on a file or directory. Change permissions in absolute mode by using the chmod command.

How do I see file permissions in Python?

stat is the right way to get more general info about a file, including permissions per user, group, and others. The st_mode attribute of the object that os. stat returns has the permission bits for the file.

What does chmod do in Python?

chmod() method in Python is used to change the mode of path to the numeric mode. mode – mode may take one of the following values: stat. S_ISUID : Set user ID on execution.


1 Answers

os.chmod(path, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) 

stat

The following flags can also be used in the mode argument of os.chmod():

stat.S_ISUID Set UID bit.

stat.S_ISGID Set-group-ID bit. This bit has several special uses. For a directory it indicates that BSD semantics is to be used for that directory: files created there inherit their group ID from the directory, not from the effective group ID of the creating process, and directories created there will also get the S_ISGID bit set. For a file that does not have the group execution bit (S_IXGRP) set, the set-group-ID bit indicates mandatory file/record locking (see also S_ENFMT).

stat.S_ISVTX Sticky bit. When this bit is set on a directory it means that a file in that directory can be renamed or deleted only by the owner of the file, by the owner of the directory, or by a privileged process.

stat.S_IRWXU Mask for file owner permissions.

stat.S_IRUSR Owner has read permission.

stat.S_IWUSR Owner has write permission.

stat.S_IXUSR Owner has execute permission.

stat.S_IRWXG Mask for group permissions.

stat.S_IRGRP Group has read permission.

stat.S_IWGRP Group has write permission.

stat.S_IXGRP Group has execute permission.

stat.S_IRWXO Mask for permissions for others (not in group).

stat.S_IROTH Others have read permission.

stat.S_IWOTH Others have write permission.

stat.S_IXOTH Others have execute permission.

stat.S_ENFMT System V file locking enforcement. This flag is shared with S_ISGID: file/record locking is enforced on files that do not have the group execution bit (S_IXGRP) set.

stat.S_IREAD Unix V7 synonym for S_IRUSR.

stat.S_IWRITE Unix V7 synonym for S_IWUSR.

stat.S_IEXEC Unix V7 synonym for S_IXUSR.

like image 69
John La Rooy Avatar answered Oct 13 '22 23:10

John La Rooy