![]() |
Please help me [copy and paste file from src to dst] - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Please help me [copy and paste file from src to dst] (/thread-38775.html) |
Please help me [copy and paste file from src to dst] - midomarc - Nov-22-2022 I need to copy and paste a file with certain extensions from one location to another and receive notify by Mail from outlook if file it doesn't already exist in the destination folder ![]() NB* : I don't want to be copying everything every single time, just those files that don't exist in the destination folder. My code import glob import os import shutil import os import win32com.client as win32 # Outlook application instance olApp = win32.Dispatch('Outlook.Application') olNS = olApp.GetNameSpace('MAPI') # path src = '/Users/Administrator/Desktop/A' dst = '/Users/Administrator/Desktop/B' # check if the file exists in dst with extensions file = os.path.join(os.path.join(src, "*.txt")) if os.path.exists(dst): for file in glob.iglob(os.path.join(src, "*.txt")): if os.path.isfile(file): shutil.copy2(file, dst) mailItem = olApp.CreateItem(0) mailItem.Subject = 'Check file' mailItem.BodyFormat = 1 mailItem.Body = "File is Successful Copy" mailItem.To = '<[email protected]>' #mailItem.Attachments.Add() mailItem.send else: print(" Skipped: already file exists ") RE: Please help me [copy and paste file from src to dst] - Pedroski55 - Nov-23-2022 Doesn't look too daunting! For emailing you could look here. import os import shutil # a lot of .txt text files here path2source = '/home/pedro/winter2022/21BE/correctAnswersCW/' # nothing in here right now path2destination = '/home/pedro/tmp/' source = os.listdir(path2source) source_files = [] for s in source: if s.endswith('.txt'): source_files.append(s) destination = os.listdir(path2destination) # read what is in the destination directory first # don't get directories destination_files = [] for d in destination: if os.path.isfile(path2destination + d): destination_files.append(d) # maybe you want to check the metadata, to see if the source file is newer than an existing file # not done that here # can copy directories with .copytree() # make a function send_an_email(file_name) to send the mail for s in source_files: if not s in destination_files: shutil.copy(path2source + s, path2destination + s) print('copied', s) send_an_email(s) # for sending an email using SMTP_SSL(), look here, seems straightforward # https://www.courier.com/blog/three-ways-to-send-emails-using-python-with-code-tutorials/ RE: Please help me [copy and paste file from src to dst] - midomarc - Nov-24-2022 (Nov-23-2022, 05:00 AM)Pedroski55 Wrote: Doesn't look too daunting! I am grateful for your help. |