GH avatar Bite 223. Unix file permissions

Each file and directory in Unix has its permissions broken down into owner, group and other (world) attributes, see here.

Each attribue has one or more permissions: r(ead), w(rite), e(x)ecute. The lack of any permission is indicated by a dash (-). So r-- = read only, rw- = read + write, and rwx = full permissions.

The file permission for owner + group + other has 3 of those strings after the initial character which indicates file type (- = file / d = directory):
$ lt bite.html
-rw-r--r--  1 bobbelderbos  staff   229B Oct  9 14:03 bite.html

To change the file permission you use the chmod and its octal representation, for each attribute (owner, group, other) we translate the permission string summing a value for each permission: r = 4, w = 2, and x = 1, hence:

$ touch myfile
$ lt myfile
-rw-r--r--  1 bobbelderbos  staff     0B Oct 11 10:12 myfile
$ chmod 400 myfile
$ lt myfile
-r--------  1 bobbelderbos  staff     0B Oct 11 10:12 myfile
$ chmod 600 myfile
$ lt myfile
-rw-------  1 bobbelderbos  staff     0B Oct 11 10:12 myfile
$ chmod 700 myfile
$ lt myfile
-rwx------  1 bobbelderbos  staff     0B Oct 11 10:12 myfile
$ chmod 740 myfile
$ lt myfile
-rwxr-----  1 bobbelderbos  staff     0B Oct 11 10:12 myfile
$ chmod 745 myfile
$ lt myfile
-rwxr--r-x  1 bobbelderbos  staff     0B Oct 11 10:12 myfile

That concludes our little Unix file permission lesson. In this Bite you will complete get_octal_from_file_permission that takes a permission string and returns its octal representation, for example:

>>> from permissions import get_octal_from_file_permission
>>> get_octal_from_file_permission('rw-r--r--')
'644'
>>> get_octal_from_file_permission('rwxrwxrwx')
'777'

Of course the tests check a bunch more (wonder about parametrize? You can read more about it on our blog - bullet 9). Have fun and keep calm and code in Python!

Login and get coding
go back Intermediate level
Bitecoin 3X

31 out of 31 users completed this Bite.
Will you be Pythonista #32 to crack this Bite?
Resolution time: ~38 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 3.0 on a 1-10 difficulty scale.
» You can do it! 😌

We use Python 3.7