w3resource

Python: Get the size, permissions, owner, device, created, last modified and last accessed date time of a specified path

Python Operating System Services: Exercise-5 with Solution

Write a Python program to get the size, permissions, owner, device, created, last modified and last accessed date and time of a specified path.

Sample Solution:

Python Code :

import os
import sys
import time
path = 'g:\\testpath\\'
print('Path Name ({}):'.format(path))
print('Size:', stat_info.st_size)
print('Permissions:', oct(stat_info.st_mode))
print('Owner:', stat_info.st_uid)
print('Device:', stat_info.st_dev)
print('Created     :', time.ctime(stat_info.st_ctime))
print('Last modified:', time.ctime(stat_info.st_mtime))
print('Last accessed:', time.ctime(stat_info.st_atime))

Sample Output:

Path Name (g:\testpath\):
Size: 546
Permissions: 0o100666
Owner: 0
Device: 4235770541
Created      : Wed Jan 15 11:21:42 2020
Last modified: Thu Jan 16 15:32:11 2020
Last accessed: Thu Jan 16 15:32:11 2020

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to check for access to a specified path. Test the existence, readability, writability and executability of the specified path.
Next: Write a Python program to create a symbolic link and read it to decide the original file pointed by the link.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/os/python-os-exercise-5.php