2

Tengo los siguientes archivos estan nombrados por mes, dia ejemplo:(1001) y de la letra 'a' a 'x'

stax1001g      stax1001n  
stax1001s      stax1001l  
stax1001w      stax1001u  
stax1001c      stax1001p  
stax1001b      stax1001h  
stax1001o      stax1001a  
stax1001v      stax1001t  
stax1001e      stax1001f  
stax1001d      stax1001q  
stax1001k      stax1001r  
stax1002d      stax1001i  
stax0206i      stax1003d  
stax1001j      stax1001m  
stax1001x

Por ejemplo quisiera yo pasar todos los archivos del dia 1001a hasta 1001x que son 24 archivos

stax1001g       stax1001n  
stax1001s       stax1001l  
stax1001w       stax1001u  
stax1001c       stax1001p  
stax1001        stax1001h  
stax1001o       stax1001a  
stax1001v       stax1001t  
stax1001e       stax1001f  
stax1001d       stax1001q  
stax1001k       stax1001r  
stax1001i       stax1001j
stax1001m       stax1001x

Ignorando los otros que archivos que no estan completos como por ejemplo estos

stax1002d 
stax0206i       stax1003d  

Este es mi codigo hasta ahora en el que solo listo los archivos del directorio, comparo si el archivo termina en la letra de la a a la x

import os
import shutil 
import re

remoto_test = "/home/User/shared/home/"
path_destino = "/home/User2/Documents/test/homefiles/"


for listado_raw in os.listdir(remoto_test):
    mo = re.match('.+([0-9])[^0-9]*$', listado_raw)
    x = mo.start(1)
    start = x + 1
    nombre = listado_raw[0:start]
    print nombre
    print listado_raw
    a = "a"
    b = "b"
    c = "c"
    d = "d"
    e = "e"
    f = "f"
    g = "g"
    h = "h"
    i = "i" 
    j = "j"
    k = "k"
    l = "l"
    m = "m"
    n = "n"
    o = "o"
    p = "p"
    q = "q"
    r = "r"
    s = "s"
    t = "t"
    u = "u"
    v = "v"
    w = "w"
    x = "x"
    i=0
    if listado_raw.endswith('a' and 'b' and 'c' and 'd' and 'e' and 'f' and 'g' and 'h' and 'i' and 'j' and 'k 'and 'l' and 'm' and 'n' and 'o' and 'p' and 'q' and 'r' and 's' and 't' and 'u' and 'v' and 'w' and 'x'):
        i=i+1
        print listado_raw
        print i
        if i == 24:
            print "muevo archivos ==>"
            #print listado_raw
            try:
                shutil.move(listado_raw,path_principal)

            except shutil.Error as e:
                msg = 'Error: %s' % e
                print msg
1
  • Antes de darte una respuesta, quisiera comentarte algo del código. Al método .endswith le estás metiendo una expresión booleana. Por culpa de como evalua python las expresiones booleanas, toda la expresión 'a' and 'b' and 'c' and 'd' and 'e' and 'f' and 'g' and 'h' and 'i' and 'j' and 'k 'and 'l' and 'm' and 'n' and 'o' and 'p' and 'q' and 'r' and 's' and 't' and 'u' and 'v' and 'w' and 'x' equivale a 'x'. Además, estás definiendo un montón de variables para una sola letra que no usas, y las que usas es porque se machacan en otro lado (x, e, i). el 13 feb. 2017 a las 21:19

1 respuesta 1

0

Comenzando, para poder contar la cantidad de datos llevas en puedes usar el enumerate, que te sirve para indicar un indice sobre el item que se esta iterando.
La lista de letras que posees es innecesaria, si deseas omitir archivos, di que no quieres tales archivos: if not te aseguras de decir eso, si no es "y" o "z", haz lo siquiente.
Para la cantidad de datos que deseas mover indica mientras sea menor que n haga otra acción.
En el move, tienes que indicar el directorio final, es decir el path incluyendo el nombre del archivo.
Ahora si deseas contar la cantidad de datos que haz movido, ahi si puedes usar un contador que aumente dentro del shutil, ya que este mueve por archivo y no está moviendo el directorio completo.

import os
import shutil
import re

remoto_test = "/home/User/shared/home/"
path_destino = "/home/User2/Documents/test/homefiles/"

for i, listado_raw in enumerate(os.listdir(remoto_test)):
    mo = re.match('.+([0-9])[^0-9]*$', listado_raw)
    x = mo.start(1)
    start = x + 1
    nombre = listado_raw[0:start]
    if not listado_raw.endswith('y' or 'z'):
        if i < 24:
            print "se mueve el archivo ==>", listado_raw
            try:
                shutil.move(remoto_test+listado_raw,path_destino)
            except shutil.Error as e:
                print 'Error: %s' % e

Espero te sirva la ayuda :) suerte en tu código.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.