Código de Python - Búsqueda de archivos en sistema, mediante expresiones regulares

Requerimientos

Lenguaje: Python
Librerías y recursos: re, os, colorama

1.0

Actualizado el 5 de Abril del 2022 (Publicado el 27 de Marzo del 2022)gráfica de visualizaciones de la versión: 1.0
1.360 visualizaciones desde el 27 de Marzo del 2022
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from colorama import init, Fore, Back, Style
import re
 
def BMP(s):
    return "".join((i if ord(i) < 10000 else '\ufffd' for i in s))
 
def ns(c):
    while c.lower()!=("s") and c.lower()!=("n"):
        c=input("Escribe solo \'n/N\' o \'s/S\' según su opción: ")
    return(c)
 
def change_dir():
    while True:
        dire = input("Introduzca directorio base: ").strip()
        if os.path.isdir(dire):
            os.chdir(dire)
            break
        else:
            print(Fore.RED+"ERROR, DIRECTORIO NO VÁLIDO"+Fore.RESET)
 
def show_dir(direc):
    global showed_dir
    if showed_dir == False:
        print(Fore.BLUE+Back.WHITE+direc+Fore.RESET+Back.RESET)
        showed_dir = True
 
conti = "s"
init()
 
while conti.lower() == "s":
    print(Back.BLUE+"\n--------------------------FILE FINDER WITH REGEX--------------------------")
    print(Back.RESET+"")
    print("Directorio actual: {} ".format(os.getcwd()))
    count = 0
    showed_dir = False
 
    change_dir()
    texto_entrada = BMP(input("Introduce patrón de búsqueda: "))
    print("BUSCANDO...\n")
    try:
        for root, folders, files in os.walk(os.getcwd()):
            for file in files:
                match_ = re.search(texto_entrada, file)
                if match_:
                    show_dir(root)
                    count+=1
                    print(Fore.GREEN+'{}-'.format(count)+os.path.join(root,BMP(Fore.YELLOW+Style.DIM+file+Fore.RESET+Style.NORMAL)))
            showed_dir = False
 
 
        if count == 0:
            print(Fore.BLACK+Back.RED+"No se encontraron coincidencias con \'{}\'.".format(texto_entrada))
        else:
            if count == 1:
                print(Fore.BLACK+Back.GREEN+"\n1 ARCHIVO ENCONTRADO.")
            else:
                print(Fore.BLACK+Back.GREEN+"\n{} ARCHIVOS ENCONTRADOS.".format(count))
 
    except Exception as e:
        print(Fore.BLACK+Back.RED+'ERROR: {} '.format(str(e)))
 
    print(Fore.RESET+Back.RESET+"")
 
    conti = ns(input("¿Continuar(n/s)?: "))



Comentarios sobre la versión: 1.0 (0)


No hay comentarios
 

Comentar la versión: 1.0

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s7216