Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Files and directories in Python on linux

I am looking for a code in python that allows me to take a directory and create a list similar to “ls –lR” containing all the files in the dir. and sub-dir. in a tabular form with their names, size, date last modified, permissions, users etc.

And also the total size of all the files in the directories and sub-directories.

I have used stats as of now, for the time, size and permissions and have appended the same in lists of which I have made tables. But to find the file name, the perm, owner and group. If I could get a neater code?

like image 359
simplycurious Avatar asked Dec 17 '22 03:12

simplycurious


1 Answers

To traverse files recursively, you can use os.walk. That will let you know all the file names that you need.

Once you have the file names, you can keep on using os.stat to get the user and group ids. Regarding permissions, I think there isn't any way to get them directly using the standard library, but you can use os.access to test permissions one by one to find out which ones are set.

like image 168
jcollado Avatar answered Dec 30 '22 01:12

jcollado