Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a stat output to a unix permissions string

Tags:

python

If you run os.stat(path) on a file and then take its st_mode parameter, how do you get from there to a string like this: rw-r--r-- as known from the Unix world?

like image 947
javex Avatar asked Jul 23 '13 11:07

javex


1 Answers

Since Python 3.3 you could use stat.filemode:

In [7]: import os, stat

In [8]: print(stat.filemode(os.stat('/home/soon/foo').st_mode))
-rw-r--r--

In [9]: ls -l ~/foo
-rw-r--r-- 1 soon users 0 Jul 23 18:15 /home/soon/foo
like image 180
awesoon Avatar answered Sep 28 '22 04:09

awesoon