Python Forum
Please help me [copy and paste file from src to dst]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help me [copy and paste file from src to dst]
#1
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
Exclamation if already file exists in the destination folder : Skip and no send any notification

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 ")
Reply
#2
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/
Reply
#3
(Nov-23-2022, 05:00 AM)Pedroski55 Wrote: 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/


I am grateful for your help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy Paste excel files based on the first letters of the file name Viento 2 346 Feb-07-2024, 12:24 PM
Last Post: Viento
  What script to paste folders thenewcoder 1 637 Nov-29-2023, 09:40 AM
Last Post: Pedroski55
  Copy data from Excel and paste into Discord (Midjourney) Joe_Wright 4 1,922 Jun-06-2023, 05:49 PM
Last Post: rajeshgk
  is it possible to copy image from email and place into excel file? cubangt 3 1,212 Nov-30-2022, 05:11 PM
Last Post: snippsat
  ImageTk Paste KDog 14 6,744 Jun-27-2021, 11:07 AM
Last Post: KDog
  Copy column from one existing excel file to another file mkujawsk 0 5,483 Apr-14-2021, 06:33 PM
Last Post: mkujawsk
  Cut and Paste Oshadha 3 2,386 Jan-20-2021, 04:27 PM
Last Post: spaceraiders
  Failing to copy file from a network to a local drive tester_V 4 6,971 Jan-20-2021, 07:40 AM
Last Post: tester_V
  Copy mp3 file multiple times and rename Mar10 4 3,671 Sep-23-2020, 01:09 AM
Last Post: Mar10
  Python Cut/Copy paste file from folder to another folder rdDrp 4 4,943 Aug-19-2020, 12:40 PM
Last Post: rdDrp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020