Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add commonly used filesystem file routines #5

Open
fstab50 opened this issue Jul 4, 2024 · 0 comments
Open

Add commonly used filesystem file routines #5

fstab50 opened this issue Jul 4, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@fstab50
Copy link
Owner

fstab50 commented Jul 4, 2024

FILE COUNT in a directory:

import os

def file_count(directory):
    """
    Returns a count of the number of files in a directory
    """
    count = 0
    for root, dirs, files in os.walk(directory):
        for file in files:
            count = count + 1
    return count

FILE AGE

import datetime, os

def filesystem_age_days(fname=''):
    """Returns the age in days of the file or folder given as a parameter"""
    if not fname:
        return 0    # No files in directory; fname is blank
    st = os.stat(fname)
    dt_modified = datetime.datetime.fromtimestamp(st.st_mtime)
    # calc age
    now = datetime.datetime.now()
    age = convert_dt_seconds((now - dt_modified).seconds)
    return age
@fstab50 fstab50 added the enhancement New feature or request label Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant