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 # instalar con python -m pip install Tabulate
- ruta = input("Ingrese un directorio: ")
- ######### listamos todos sus archivos
- print("\nArchivos")
- total = 0
- for entrada in os.scandir(ruta):
- if entrada.is_file():
- print(entrada.name)
- total += 1
- print(total)
- ######### listamos todos sus directorios
- print("\nCarpetas")
- total = 0
- for entrada in os.scandir(ruta):
- if entrada.is_dir():
- print(entrada.name)
- total += 1
- print(total)
- ###### Mostrar archivos con tamaño y fecha de ultima modificacion
- tabla = []
- for entrada in os.scandir(ruta):
- if entrada.is_file():
- info = entrada.stat()
- tamaño = info.st_size
- if tamaño < 1024:
- tamaño = f"{tamaño} B"
- elif 1024 <= tamaño << 1024**2:
- tamaño = "{:.1f} KB".format(tamaño/1024)
- else:
- tamaño = "{:.1f} MB".format(tamaño/1024**2)
- ult_mod = datetime.utcfromtimestamp(info.st_mtime).strftime("%d-%b-%y %H:%M")
- tabla.append([entrada.name, tamaño, ult_mod])
- print(tabulate(
- tabla,
- headers = ['Archivo', 'Tamaño', 'Ultima modificación'],
- tablefmt = 'grid',
- colalign = ['left', 'right', 'center']
- )
- )
Advertisement
Add Comment
Please, Sign In to add comment