File Creation Age Calculator

  • Share this:

Code introduction


Calculate the age of a given file in days from its creation to the current time.


Technology Stack : os, re, sys, time, datetime, random, json

Code Type : Function

Code Difficulty : Intermediate


                
                    
import os
import re
import sys
import time
import datetime
import random
import json

def file_age(file_path):
    if not os.path.exists(file_path):
        return None
    ctime = os.path.getctime(file_path)
    mtime = os.path.getmtime(file_path)
    now = datetime.datetime.now()
    age_seconds = (now - datetime.datetime.fromtimestamp(ctime)).total_seconds()
    age_minutes = age_seconds / 60
    age_hours = age_minutes / 60
    age_days = age_hours / 24
    return age_days