У меня есть сценарий python, в котором перечислены папки и файлы, существующие по заданному пути.
Я хочу проверить, начинается ли существующая папка со строки "pdf" + date вот так: pdf 18-19-06-2020 . Если папка начинается только с pdf и дата не в формате «дд-дд-мм-гггг», мне нужно преобразовать имя в требуемый формат.
Я получаю текущая дата и вчерашняя дата .
код:
#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()