Advertisement
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
- directorio = input("Ingrese un directorio: ")
- ###### listado de todos los archivos en un directorio ###########
- total = 0
- print("\nArchivos")
- for elemento in os.scandir(directorio):
- if elemento.is_file():
- print(elemento.name)
- total += 1
- print(f"\nTotal: {total}")
- ##### listado de todos los subdirectorios ################
- total = 0
- print("\n\nSubdirectorios")
- for elemento in os.scandir(directorio):
- if elemento.is_dir():
- print(elemento.name)
- total += 1
- print(f"\nTotal: {total}")
- #### para mostrar los archivos con tamaño y fecha de modificación
- tabla = []
- for elemento in os.scandir(directorio):
- if elemento.is_file():
- tamanio = elemento.stat().st_size
- if tamanio < 1024:
- tamanio = f"{tamanio} B"
- elif 1024 <= tamanio < 1024**2:
- tamanio = "{:.1f} KB".format(tamanio / 1024)
- else:
- tamanio = "{:.1f} MB".format(tamanio / 1024**2)
- ult_mod = datetime.utcfromtimestamp(elemento.stat().st_mtime).strftime("%d-%b-%y %H:%M")
- tabla.append([elemento.name, tamanio, 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
Advertisement