Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- """
- Script que lista los archivos dentro de un directorio
- """
- import os
- import sys
- from datetime import datetime
- from tabulate import tabulate
- # verifico la cantidad de argumentos
- if len(sys.argv) != 2:
- sys.exit("Error. Debe ingresar 'python listar.py <ruta>'")
- # creo una tabla para almacenar la información a mostrar
- tabla = []
- # asigno los parámetros
- ruta = sys.argv[1]
- # verifico la ruta
- if not os.path.exists(ruta):
- sys.exit(f"Error: no existe la ruta '{ruta}'")
- for entrada in os.scandir(ruta):
- if entrada.is_file():
- info = entrada.stat()
- tam = info.st_size
- if tam < 1024:
- tam = f"{tam} B"
- elif 1024 <= tam < 1024**2:
- tam = f"{tam/1024:.1f} KB"
- else:
- tam = f"{tam/1024**2:.1f} MB"
- ult_mod = datetime.utcfromtimestamp(info.st_time).strftime("%d-%b-%y %H:%M")
- tabla.append([entrada.name, tam, 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