Python - Automatizar carpetas en python

 
Vista:
sin imagen de perfil

Automatizar carpetas en python

Publicado por Rodrigo (1 intervención) el 07/07/2021 21:01:33
Hola no me funciona el codigo si alguin me podria ayudar

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
import os
import shutil
import time
 
ruta_descargas = 'C:\\Users\\rovel\\Downloads'
temporizador = 30
 
ext_vid = ['.mov', '.mp4', '.avi', '.mkv', '.mkv', '.flv', '.wmv']
ext_aud = ['.mp3', '.wma', '.wav', '.flac']
ext_img = ['.jpg', '.png', 'jpeg', '.gif', '.tiff', '.psd', '.bmp', '.ico', '.svg']
ext_doc = ['.txt', '.doc', '.docx', 'pptx', '.odf', '.docm', '.pdf']
ext_com = ['.zip', '.rar', '.rar5', '.7z', '.ace', '.gz']
ext_ins = ['.exe', '.msi']
 
def motor(archivo, ext):
    for i in ext_doc:
        if ext == i:
            shutil.move(ruta_descargas + archivo + i, ruta_descargas + 'Documentos')
 
    for i in ext_aud:
        if ext == i:
            shutil.move(ruta_descargas + archivo + i, ruta_descargas + 'Audio')
 
    for i in ext_img:
        if ext == i:
            shutil.move(ruta_descargas + archivo + i, ruta_descargas + 'Imagenes')
 
    for i in ext_doc:
        if ext == i:
            shutil.move(ruta_descargas + archivo + i, ruta_descargas + 'Documentos')
 
    for i in ext_ins:
        if ext == i:
            shutil.move(ruta_descargas + archivo + i, ruta_descargas + 'Instaladores')
 
    for i in ext_com:
        if ext == i:
            shutil.move(ruta_descargas + archivo + i, ruta_descargas + 'Comprimidos')
 
    if ext != '':
        shutil.move(ruta_descargas + archivo + i, ruta_descargas + 'Otros')
 
def verificar():
    for archivo in os.listdir(ruta_descargas):
        nombre_archivo, ext = os.path.splitext(archivo)
        motor(nombre_archivo, ext)
 
while(True):
    try:
        time.sleep(temporizador)
        for _ in range(len(os.listdir(ruta_descargas))):
            try:
                verificar()
            except:
                pass
    except KeyboardInterrupt:
        break
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 2.808
Oro
Ha mantenido su posición en Python (en relación al último mes)
Gráfica de Python

Automatizar carpetas en python

Publicado por tincopasan (1082 intervenciones) el 07/07/2021 22:22:23
hay cosas mal, repetitivas o innecesarias:
1) ¿para qué un temporizador y encima con tanta espera?
2) si bien esto no está mal, es mejor escribirlo en python:
1
2
ruta_descargas = 'C:\\Users\\rovel\\Downloads'
ruta_descargas = 'C:/Users/rovel/Downloads'

donde se usa "/" en vez de "\"

3)Cuando haces la concatenación entre la ruta y el archivo hay un error porque ya viene mal la ruta y es que falta el separador.
1
ruta_descargas = 'C:/Users/rovel/Downloads/'
sería la forma correcta

4) hay una cantidad de bucles innecesarios:
1
2
3
for i in ext_doc:
        if ext == i:
            shutil.move(ruta_descargas + archivo + i, ruta_descargas + 'Documentos')
¿para qué? si ya funciona con :
1
2
if ext in ext_doc:
		shutil etc...
5) ni hablar que solo funcionará para ese usuario
6)hay un exceso de try... aunque funcionará igual.
Ya con eso tenés que dejarlo funcional.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar