Python: Get last modified information of a file
Python Datetime: Exercise-38 with Solution
Write a Python program to get last modified information of a file.
import os, time def last_modified_fileinfo(filepath): filestat = os.stat(filepath) date = time.localtime((filestat.st_mtime)) # Extract year, month and day from the date year = date month = date day = date # Extract hour, minute, second hour = date minute = date second = date # Year strYear = str(year)[0:] # Month if (month <=9): strMonth = '0' + str(month) else: strMonth = str(month) # Date if (day <=9): strDay = '0' + str(day) else: strDay = str(day) return (strYear+"-"+strMonth+"-"+strDay+" "+str(hour)+":"+str(minute)+":"+str(second)) print() print(last_modified_fileinfo('test.txt')) print()
Python Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
Test your Python skills with w3resource's quiz
Python: Tips of the Day
Free up Memory:
- Manual garbage collection can be performed on timely or event based mechanism.
import gc collected_objects = gc.collect()
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework