I'm having problems outputting timestamps for files in a directory. Want to list timestamps if they reach a certain time window, currently can't even get output. Not sure what I am doing wrong and hitting a wall atm
from datetime import datetime, timedelta
from os import scandir
DIR = "/home/user/directory"
TIME_FORMAT = "%Y-%m-%d%H:%M:%S.%fZ"
def convert_timestamps():
"""converting timestamps to human readable"""
# timestamp = datetime.utcnow() - timedelta(days=7)
timestamp = datetime.utcfromtimestamp
file_times = timestamp.strftime(TIME_FORMAT)
return file_times
def get_files():
"""getting files from dir"""
dir_filenames = scandir(DIR)
for filename in dir_filenames:
if filename.is_file():
file_stat = os.stat(str(filename))
# info = os.stat(filename)
print("Last Modified time: {}".format(filename.name, convert_timestamps(file_stat.st_mtime)))
if __name__ == 'main':
get_files()
The following snippet works, but is obviously not much of a program
DIR = "/home/user/directory"
TIME_FORMAT = "%Y-%m-%d%H:%M:%S.%fZ"
file_stat = os.stat(DIR)
timestamp = datetime.utcnow() - timedelta(days=7)
file_times = timestamp.strftime(TIME_FORMAT)
print("Last mod time are: ", file_times)