Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import os
- from datetime import datetime
- from tabulate import tabulate # biblioteca externa, pip install tabulate para instalarla
- archivos = 0
- directorios = 0
- # creo la tabla vacia
- # formato de la tabla
- # si es un archivo: <fecha y hora> <tamaño> <nombre>
- # si es un directorio: <fecha y hora> DIR <nombre>
- tabla = []
- # escaneo el directorio y leo los datos
- for item in os.scandir(os.getcwd()):
- if item.is_file():
- archivos += 1
- nombre = item.name
- # leo la info del archivo
- info = item.stat()
- # leo el tamaño del archivo
- tam = info.st_size
- # doy formato al tamaño:
- if tam < 1024:
- tam = f"{tam} B"
- elif 1024 <= tam <1024**2:
- tam = f"{tam/1024:.1f} KB"
- else:
- tam = f"{tam/1024**2:.1f} MB"
- # leo fecha y hora del ultima modificacion
- modif = datetime.utcfromtimestamp(info.st_mtime).strftime('%d/%m/%Y %H:%M')
- # agrego el dato a la tabla
- tabla.append([modif,"",tam,nombre])
- elif item.is_dir():
- directorios += 1
- nombre = item.name
- # leo fecha y hora del ultima modificacion
- modif = datetime.utcfromtimestamp(info.st_mtime).strftime('%d/%m/%Y %H:%M')
- # agrego el dato a la tabla
- tabla.append([modif,"<DIR>","",nombre])
- print(f"Directorio: {os.getcwd()}\n")
- print(tabulate(
- tabla,
- tablefmt = "plain",
- colalign = ["center","center", "right", "left"],
- )
- )
- print(f"\n\n\t\t{archivos} archivos")
- print(f"\t\t{directorios} directorios")
Advertisement
Add Comment
Please, Sign In to add comment