0

I have a python script that list folder and files existing in a given path.

What i want is to be able to check if the existing folder start with string "pdf" + date like this: pdf 18-19-06-2020. If the folder start with just pdf and the date is not in the format "dd-dd-mm-yyyy" i need to convert the name to the required format.

I am getting the current date and the date of yesterday.

code:

#packages for list and copy folders & files.
import calendar
import os
import shutil
from os import path
from datetime import date

def main():
    copy(src)

'''
FUNCTION THAT calculate current date and 2 dates before   
'''


def yesterday():
    days=[]
    day = int(date.today().strftime("%d"))
    month = int(date.today().strftime("%m"))
    year = int(date.today().strftime("%Y"))
    if day != 1:
        p = day -1
        p1 = day -2
        p2 = day -3

        print("******",p)
        print("******",p1)
        print("******",p2)
        days.append(p)
        days.append(p1)
        days.append(p2)
        print("******",days,"********")
        return p
    else:
        p = 32 -1
        print("******",p)
        return p
    long_months = [1, 3, 5, 7, 8, 10, 12]
    if month in long_months:
        print(32 -1)
        return(32-1) 
    elif month == 2:
        if calendar.isleap(year):
            return 29
        return 28
    else:
        return 30

dst = "E:/KRD2018_Data"
dst2 = "F:/ABpro" 
dst3 = "C:/Users/cvd/Documents"
'''
FUNCTION THAT list the folders and files exist on the USB drive and copy the pdfs and docs to their destinations 
and copy the pdfs in the existing folder to the specified destination
'''
def copy(src):

    #name = folder pdf yesterday + today

    #pdf dd-dd-mm-yyyy ==> 3-04-05-2020
    datefile = "pdf " + str(yesterday()) + date.today().strftime("-%d-%m-%Y")
    src2 = os.path.join(src, datefile)
    ignore_list=["$RECYCLE.BIN","System Volume Information"]

    i=0
    j=0
    z=0

    for dirpath, dirnames, files in os.walk(src, topdown=True):         
        print(f'Found directory: {dirpath}')
        if len(dirnames)==0 and len(files)==0:
            print("this directory is empty")
            continue
        # exclude the ignore list from the os.walk
        dirnames[:] = [d  for d in dirnames if d not in ignore_list]
        # check if the path is directory
        isdir  = os.path.isdir(dirpath)
        print(isdir)


        for  file in files:
            full_file_name = os.path.join(dirpath, file)
            if os.path.join(dirpath) == src:
                if file.endswith("pdf"):
                    if not os.path.exists(dst2):
                        os.mkdir(dst2)
                    else:
                        print("the path alredy exist")
                    # shutil.copy(full_file_name, dst2)
                    i+=1

                elif file.endswith("docx") or file.endswith("doc"):
                    # shutil.copy(full_file_name, dst)
                    j+=1

            elif os.path.join(dirpath)== src2:
                if file.endswith("pdf"):
                    numfile = len(files)

                    # shutil.copy(full_file_name, dst3)
                    z+=1
        print("*******number of directories = {}".format(len(dirnames)))
        print("*******number of files = {}".format(len(files)))
    print("{} word file \n".format(j))
    print("{} pdf files \n".format(z))
    print("{} other files \n".format(i))
    print("total copied files {}".format(i+j+z))
if __name__=="__main__":
    main()  
Pyleb Pyl3b
  • 183
  • 3
  • 16
  • What is the problem and where does the expected output not match with what you get? You can do pattern matching using `re`. What are possible formats you need to detect? – po.pe Jun 19 '20 at 07:47
  • the problem is how to check if the founded folder name start with "PDF" and how to rename the folder to match the format of the requested name "pdf dd-dd-mm-yyy" – Pyleb Pyl3b Jun 19 '20 at 07:49
  • in my code i am not able to check if the folder name start with "pdf" i can do it to the files but not folders . – Pyleb Pyl3b Jun 19 '20 at 07:50
  • So the problem is to get a list of foldernames? https://stackoverflow.com/questions/973473/getting-a-list-of-all-subdirectories-in-the-current-directory – po.pe Jun 19 '20 at 07:51
  • @po.pe this is not what i am looking for – Pyleb Pyl3b Jun 19 '20 at 08:01
  • Then what are you looking for? Your problem is not clear to me :) – po.pe Jun 19 '20 at 08:37
  • @po.pe ok i am able to list folders and files but what i want is to check if the folder name start with string. example **pdf 18-19-06-2020** if its just start with **pdf** but different date format i want to change the name to match the format above. else if does not start with **pdf** i do not want to show it. – Pyleb Pyl3b Jun 19 '20 at 08:42

0 Answers0