w3resource

Python: Retrieve file properties

Python Basic: Exercise-107 with Solution

Write a Python program to retrieve file properties.

Sample Solution:

Python Code:

# Import the 'os.path' and 'time' modules for working with file paths and time-related functions.
import os.path
import time
# Print the name of the current file using '__file__' attribute.
print('File         :', __file__)

# Print the access time of the current file using 'os.path.getatime()' and 'time.ctime()'.
print('Access time  :', time.ctime(os.path.getatime(__file__)))

# Print the modified time of the current file using 'os.path.getmtime()' and 'time.ctime()'.
print('Modified time:', time.ctime(os.path.getmtime(__file__)))

# Print the change time of the current file using 'os.path.getctime()' and 'time.ctime()'.
print('Change time  :', time.ctime(os.path.getctime(__file__)))

# Print the size of the current file using 'os.path.getsize()'.
print('Size         :', os.path.getsize(__file__))

Sample Output:

File         : 8dbacd90-266d-11e7-a9c1-cf681af3cdf1.py             
Access time  : Fri Apr 21 14:06:03 2017                            
Modified time: Fri Apr 21 14:06:02 2017                            
Change time  : Fri Apr 21 14:06:02 2017                            
Size         : 304   

Flowchart:

Flowchart: Retrieve file properties.

Python Code Editor:

 

Previous: Write a Python program to divide a path on the extension separator.
Next: Write a Python program to find path refers to a file or directory when you encounter a path name.

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/python-basic-exercise-107.php