Advertisement
teslariu

listar

Jun 30th, 2022
1,159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. Script que lista los archivos dentro de un directorio
  6. """
  7. import os
  8. import sys
  9. from datetime import datetime
  10. from tabulate import tabulate
  11.  
  12. # verifico la cantidad de argumentos
  13. if len(sys.argv) != 2:
  14.     sys.exit("Error. Debe ingresar 'python listar.py <ruta>'")
  15.  
  16. # creo una tabla para almacenar la información a mostrar
  17. tabla = []
  18.  
  19. # asigno los parámetros
  20. ruta = sys.argv[1]
  21.  
  22. # verifico la ruta
  23. if not os.path.exists(ruta):
  24.     sys.exit(f"Error: no existe la ruta '{ruta}'")
  25.  
  26. for entrada in os.scandir(ruta):
  27.     if entrada.is_file():
  28.         info = entrada.stat()
  29.         tam = info.st_size
  30.        
  31.         if tam < 1024:
  32.             tam = f"{tam} B"
  33.         elif 1024 <= tam < 1024**2:
  34.             tam = f"{tam/1024:.1f} KB"
  35.         else:
  36.             tam = f"{tam/1024**2:.1f} MB"
  37.        
  38.         ult_mod = datetime.utcfromtimestamp(info.st_time).strftime("%d-%b-%y %H:%M")
  39.         tabla.append([entrada.name, tam, ult_mod])
  40.  
  41. print(tabulate(
  42.         tabla,
  43.         headers = ["Archivo", "Tamaño", "Ultima modificación"],
  44.         tablefmt = "grid",
  45.         colalign = ["left", "right", "center"]
  46.         )
  47.     )
  48.    
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement