Advertisement
teslariu

escan

Sep 25th, 2021
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 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. ############ lista de los archivos ######
  11. print("\nArchivos:")
  12. print("---------------------------------")
  13. for entrada in os.scandir(directorio):
  14.     if entrada.is_file():
  15.         print(entrada.name)
  16.  
  17. ############ lista de las subcarpetas ######
  18. print("\nCarpetas:")
  19. print("---------------------------------")
  20. for entrada in os.scandir(directorio):
  21.     if entrada.is_dir():
  22.         print(entrada.name)
  23.  
  24. ########## mostrar archivos con tamaño y ultima modificación
  25. # creo una variable tabla para poder imprimir los datos
  26. tabla = []
  27. for entrada in os.scandir(directorio):
  28.     if entrada.is_file():
  29.         info = entrada.stat()
  30.         tamaño = info.st_size
  31.        
  32.         if tamaño < 1024:
  33.             tamaño = f"{tamaño} B"
  34.        
  35.         elif 1024 < tamaño < 1024**2:
  36.             tamaño = "{:.1f} KB".format(tamaño/1024)
  37.            
  38.         else:
  39.             tamaño = "{:.1f} MB".format(tamaño/(1024**2))
  40.            
  41.         ult_modificacion = datetime.utcfromtimestamp(info.st_mtime).strftime('%d-%b-%y %H:%M')
  42.        
  43.         # agrego los datos del archivo a la tabla
  44.         tabla.append([entrada.name, tamaño, ult_modificacion])
  45.        
  46. print(tabulate(
  47.                 tabla,
  48.                 headers = ["Archivos","Tamaño","Ultima modificación"],
  49.                 tablefmt = 'grid',
  50.                 colalign = ['left', 'right', 'center']
  51.         )
  52.     )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement