0

I want to write a script that copies the contents of one directory and back them up in a different one. For instance, I want to back up the contents of directory Bye to directory Cheese.

I also want the script to work in such a way that the scheduled task is repeated daily at a specific time, say, 8pm. The existing folders should merge with the ones being copied and the new files should replace the existing ones.

mondieki
  • 1,771
  • 3
  • 16
  • 24
  • 1
    possible duplicate of [How do I copy a file in python?](http://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python) – nofinator Jul 27 '15 at 15:59
  • 1
    Perhaps [rsync](https://en.wikipedia.org/wiki/Rsync) is better suited for this task. – pzp Jul 27 '15 at 16:18

3 Answers3

1

You'll need an event scheduler. Install the schedule module first, i.e:

pip install schedule

This should do the job:

import os
import schedule
import shutil
import time

def job():
    source_folder = r'C:\Users\User\Desktop\Bye'    
    destination_folder = 'C:\Users\User\Desktop\Cheese'  

    for src_dir, dirs, files in os.walk(source_folder):
        dst_dir = src_dir.replace(source_folder, destination_folder, 1)
        if not os.path.exists(dst_dir):
            os.makedirs(dst_dir)
        for file_ in files:
            src_file = os.path.join(src_dir, file_)
            dst_file = os.path.join(dst_dir, file_)
            if os.path.exists(dst_file):
                os.remove(dst_file)
            shutil.copy(src_file, dst_dir)

schedule.every().day.at("20:00").do(job)

while 1:
    schedule.run_pending()
    time.sleep(1)
mondieki
  • 1,771
  • 3
  • 16
  • 24
0

Have a look at the shutil library: https://docs.python.org/2/library/shutil.html

e.g.

>>> import shutil
>>> shutil.copyfile('foo.txt', 'bar.txt')
cchristelis
  • 1,985
  • 1
  • 13
  • 17
  • there is hundreds of files and what i understand by the code you gave in the example it says that you need to name all the files. One thing is that there is new files being added to the server on a daily basis so i need a piece of code that will: -copy the whole contents of the folder to a different server on the system and replace all the files with the same name -But also set the code to run at a specific time of day – Karol Kowalczyk Jul 27 '15 at 16:06
  • You probably know the directory? If so list the directory and copy each file in the list, or copy the directory across. – cchristelis Jul 27 '15 at 16:09
0

This is interesting.

I'd suggest you to research a bit. I found one Python Module that works as you ask, it's called shutil (Docs) and this post explains a bit on how to use it with examples. Also, several related questions: How do I copy a file in python?, How do I copy a file in python?, Copy file or directory in Python. There you have several examples on how to use that module.

Do you know how to use modules? The docs just give you the basics on the module functions. It might be confusing if you're new in programming as I am. But the examples I linked to similar questions will guide you a lot!

I understand you need to solve it ASAP but research a bit and try to solve it. Once you've some basic code it's easier to help you.

Cheers!!

Community
  • 1
  • 1
egodial
  • 87
  • 1
  • 2
  • 9