Aug-15-2017, 07:40 PM
Hi, how can i copy all files with extension ".txt " and ".pdf"?
theres a problem in my code at the time i want to copy certain files, only copy just one single file to my
tmp folder and the folder contains multiples .txt files
any idea why?
this is my code so far:
theres a problem in my code at the time i want to copy certain files, only copy just one single file to my
tmp folder and the folder contains multiples .txt files
any idea why?
this is my code so far:
print "busca status en 0" sql = "select * from solicitudes where status = 0" try: cursor.execute(sql) solicitudes = cursor.fetchall() except: print "error unable to fetch data" pathloc = "/home/user/Documents/data/files/" spath = "datosFolder/" ProcesaStatus(solicitudes) def ProcesaStatus(solicitudes): for solicitud in solicitudes: idSol = solicitud[0] mailSol = solicitud[5] sql = "select * from evtSolicitudes where idSol = %d"%(idSol) zip_file_name = 'solicitud_' + str(idSol) + '.zip' z = zipfile.ZipFile(zip_file_name, "w") try: cursor.execute(sql) archivos = cursor.fetchall() for file_st in archivos: archivo_st = file_st[1] print archivo_st sqlEv ="""SELECT ev.idEvt, ev.Estacion, si.Clave, st.Nombre, si.Fecha, ev.ID_Eventos FROM ranm.tblEvent si INNER JOIN ranm.tblCodes ev ON ev.idEvt = si.ID_Name INNER JOIN ranm.tblSt st ON st.ID_Estaciones = ev.Estacion WHERE ev.ID_Eventos = %d"""%(int(archivo_st)) try: cursor.execute(sqlEv) fid = cursor.fetchall() for ids in fid: numEvt = ids[0] clave = ids[2] nombre = ids[3] fechas = ids[4] yr = fechas.year cut = str(fechas) month = cut[5:7] day = cut[8:10] mday = month + day fecha = str(fechas) rpath = pathloc + str(yr) + '/' + clave + '/' + nombre + '_' + mday + '/' print rpath if os.path.exists(rpath): print 'existe==>' foldertmp = tempfile.mkdtemp() nametmp = spath + nombre + '/' saved_umask = os.umask(0077) storageTemp = os.path.join(foldertmp, nametmp) print storageTemp #call functions if copypaths(storageTemp,rpath): zipfiles(storageTemp,foldertmp,spath,nombre,z) print 'zip successfully' else: print 'no existe el path' except db.Error, e: print 'error unable to fetch data' except db.Error, e: print "error unable to fetch data" z.close() z = zipfile.ZipFile(zip_file_name) def copypaths(storageTemp,rpath): for root, directories, files in os.walk(rpath): for filename in files: filepath = os.path.join(root, filename) if ( (filepath.endswith('.txt')) or (filepath.endswith('.pdf')) ): #pathr.append(filepath) print filepath if os.path.exists(storageTemp): try: print 'entro a if existe' shutil.copy(filepath, storageTemp) return True except shutil.Error as e: msg = 'Error: %s' % e print 'Error: ',msg else: try: print 'entro a else no existe folder' os.makedirs(storageTemp) shutil.copy(filepath,storageTemp) return True except shutil.Error as e: msg = 'Error: %s' % e print 'Error: ',msg def zipfiles(path_estacion, dir_temp, base_dir, name, z): print 'make Zip..' for root, dirs, files in os.walk(dir_temp): for nfile in files: currentdirectory = os.getcwd() os.chdir(dir_temp) z.write(base_dir + name + "/" + nfile) os.chdir(currentdirectory) #shutil.rmtree(path_estacion)