Reputation: 1
#[Updated] The updated code works just fine, its supposed to get file(s) named as MAYFLY, a file docx and if its today's created file but after running it, instead of get only that criteria, it grabs together with today's created file without a MAYFLY name on the File from the If statement line.
src = r"/storage/emulated/0/Leonce Python Projects"
files = glob.glob(f"{src}\**\*.docx", recursive=True)
print(files)
dst = r"/storage/emulated/0/A-MAYFLYS"
for file in files:
print(file)
ctime = time.strftime("%d %B, %Y", time.strptime(time.ctime(os.path.getctime(file))))
if file.endswith(".docx") and (to_day == ctime) and "MAYFLY" in file :
print(f"{to_day} Mayfly File Found & Exists: " + file)
print("..Copying n Renaming the file into its Destination Folder.")
dst_file = os.path.join(dst, str("MAYFLY FOR " + ctime + ".docx"))
if os.path.isfile(dst_file):
print("File Already Exists, We cannot replace it again.")
else:
shutil.copyfile(os.path.join(src, file), dst_file)
Upvotes: 0
Views: 157
Reputation: 36
First, you can get the file names in your specified folder. Then, you must get the time information of today. At last, you must check whether the time information of each file is within today.
You can use the following code:
src = r"./" # the folder path
import os,glob,time
import datetime as dt
files = os.listdir(src) # files in the specified folder
print(f"Today: {dt.datetime.now().date()}") # Today
tdt=dt.datetime.now().timetuple()
for file in files: # loop for eath file
if not file.endswith(".docx"): # if or not your requried file type
continue
tmp=time.strptime((time.ctime(os.path.getctime(file)))) # the time information of this file
if tdt[0]==tmp[0] and tdt[1]==tmp[1] and tdt[2]==tmp[2]:
print(file)
Upvotes: 1