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
#2
Your problem is the bloated code. You can't see the forest in front of loud trees.
You have two return statements inside the loop of the function copypaths. After
the first file has been copied, the function returns True.

Try not to do everything in one function. Use different functions, which are
specialized to solve one problem.

You should also refactor your functions. You need to create the temporary
directory before you copy the files. Do it before the loop starts and
then you don't need to check if the directory is already existing.
There is also the principle: don't ask for permission ask for forgiveness

def has_file_ending(filepath, endings):
    return any(os.path.splitext(filepath)[1] in end for end in endings)

def copypaths(storageTemp, rpath):
    try:
        os.makedirs(storageTemp)
    except OSError as e:
        # should we log it that there is already a
        # temporary directory?
        pass
    for root, directories, files in os.walk(rpath):
        for filename in files:
            filepath = os.path.join(root, filename)
            if has_file_ending(filepath, ['.txt', '.pdf']):
                try:
                    shutil.copy(filepath, storageTemp)
                except shutil.Error as e:
                    # when do this error happen?
                    # maybe you don't need to catch this
                    # exception
                    pass
                except IOError as e:
                    # for example
                    # the special case you want to log
                    print 'Permission error with file', filepath
                else:
                    # this block is handled
                    # when no exception happens
                    print 'File {} has been copied to {}'.format(filepath, storageTemp)
I did not checked the code for errors. Maybe there is somewhere a typo.
If you need a return value True or False for success,
you should think about it, when it should happen and in which
case you want to return False. You can push the error handling
for some reason out of the function and then the caller has to handle it.

If a caller or a callee handle an error is not always an easy answer.
About this you can see a bunch of talks.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 276 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy Paste excel files based on the first letters of the file name Viento 2 440 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 551 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Rename files in a folder named using windows explorer hitoxman 3 742 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Rename all files in a folder hitoxman 9 1,495 Jun-30-2023, 12:19 AM
Last Post: Pedroski55
  Create new folders and copy files cocobolli 3 1,472 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  Copy only hidden files and folders with rsync Cannondale 2 1,018 Mar-04-2023, 02:48 PM
Last Post: Cannondale
  How to loop through all excel files and sheets in folder jadelola 1 4,497 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  python gzip all files from a folder mg24 3 4,021 Oct-28-2022, 03:59 PM
Last Post: mg24
  delete all files and subdirectory from a main folder mg24 7 1,606 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