Advertisement
teslariu

listado

Feb 18th, 2022
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import os
  5. from datetime import datetime
  6. from tabulate import tabulate
  7.  
  8. directorio = input("Ingrese el directorio: ")
  9.  
  10. # voy a mostrar archivos con tamaño y fecha de modificacion
  11. # creo una tabla para guardar los datos
  12. tabla = []
  13.  
  14. for entrada in os.scandir(directorio):
  15.     if entrada.is_file():       # pregunto si es un archivo
  16.         info = entrada.stat()   # almaceno el estado de un archivo
  17.         tamaño = info.st_size
  18.        
  19.         if tamaño < 1024:
  20.             tamaño = f"{tamaño} B"
  21.  
  22.         elif 1024 <= tamaño < 1024**2:
  23.             tamaño = "{:.1f} KB".format(tamaño/1024)
  24.            
  25.         else:
  26.             tamaño = "{:.1f} MB".format(tamaño / 1024**2)
  27.            
  28.         ult_modif = datetime.utcfromtimestamp(info.st_mtime).strftime("%d-%b-%y %H:%M")
  29.        
  30.         tabla.append([entrada.name, tamaño, ult_modif])
  31.  
  32. print(tabulate(
  33.             tabla,
  34.             headers = ["Archivo", "Tamaño", "Ultima modificación"],
  35.             tablefmt = "grid",
  36.             colalign = ['left', 'right', 'center']
  37.             )
  38.     )
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement