Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
2.8 kB
3
Indexable
Never
import os
import sys
import glob
import time
import datetime
import itertools

fs = {}

def recurseDir(folderpath):
    if not folderpath.endswith("/"):
        folderpath += "/"
    for path in glob.glob(folderpath + '*'):
        if path == folderpath:
            continue
        if os.path.isfile(path):
            if folderpath.split("/")[-2] in fs:
                fs[folderpath.split("/")[-2]].append(os.path.basename(path))
            else:
                fs[folderpath.split("/")[-2]] = [os.path.basename(path)]
        elif os.path.isdir(path):
            recurseDir(path)

def printline():
    print("---------------------------------------------------------")

def main():
    try:
        printline()
        folderpath = input("Enter a Directory: ")
        printline()
        if os.path.exists(folderpath):
            recurseDir(folderpath)
        else:
            print("Invalid Path") 
    except:
        print("Incorrect Directory")
    headers = list(fs.keys())
    horizontal = itertools.zip_longest(*(fs.values()), fillvalue='')
    num = len(headers)
    formatString = ''
    for i in range(num):
        formatString += "{: <20} "
    print(formatString.format(*headers))
    print()
    for row in horizontal:
        print(formatString.format(*row))
    printline()
    while(True):
        try:
            inputdate = input("Enter date range in the format YYYY/MM/DD – YYYY/MM/DD: ")
            start_date = time.mktime(time.strptime(inputdate.split("-")[0].strip(), "%Y/%m/%d"))
            end_date = time.mktime(time.strptime(inputdate.split("-")[1].strip(), "%Y/%m/%d"))
            break
        except:
            print("Wrong format, try again")
            continue
    printline()
    inrange = []
    outrange = []
    if not folderpath.endswith("/"):
        folderpath += "/"
    for path in glob.glob(folderpath + '**', recursive = True):
        if os.path.isfile(path):
            if os.path.getmtime(path) >= start_date and os.path.getmtime(path) <= end_date:
                inrange.append(path)
            else:
                outrange.append(path)
    printline()
    print("Files in the given range: ")
    printline()
    for path in inrange:
        print("{file: <{mlen}}    Date: {date}".format(file = path, mlen =  len(max(inrange, key = len)), date = time.strftime('%b %d, %Y - %H:%M:%S', time.localtime(os.path.getmtime(path)))))
    printline()
    print("Files not in the given date range:")
    printline()
    for path in outrange:
        print("{file: <{mlen}}    Date: {date}".format(file = path, mlen =  len(max(inrange, key = len)), date = time.strftime('%b %d, %Y - %H:%M:%S', time.localtime(os.path.getmtime(path)))))
if __name__ == "__main__":
    main()