Python Forum
how to copy all files to my tmp folder?
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to copy all files to my tmp folder?
#1
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:

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)
Reply


Messages In This Thread
how to copy all files to my tmp folder? - by terry_arman2 - Aug-15-2017, 07:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 308 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy Paste excel files based on the first letters of the file name Viento 2 477 Feb-07-2024, 12:24 PM
Last Post: Viento
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 601 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Rename files in a folder named using windows explorer hitoxman 3 781 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Rename all files in a folder hitoxman 9 1,549 Jun-30-2023, 12:19 AM
Last Post: Pedroski55
  Create new folders and copy files cocobolli 3 1,528 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  Copy only hidden files and folders with rsync Cannondale 2 1,048 Mar-04-2023, 02:48 PM
Last Post: Cannondale
  How to loop through all excel files and sheets in folder jadelola 1 4,566 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  python gzip all files from a folder mg24 3 4,114 Oct-28-2022, 03:59 PM
Last Post: mg24
  delete all files and subdirectory from a main folder mg24 7 1,655 Oct-28-2022, 07:55 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020