School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,152 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,693 people online right now. Registration is fast and FREE... Join Now!




Python Backup Program

 

Python Backup Program, a simple program that backs up music and pictures to an external drive

gelnika

30 Sep, 2009 - 01:31 PM
Post #1

New D.I.C Head
*

Joined: 24 Sep, 2009
Posts: 1

Hello all,

i have some of the basics down for Python, but i've been working on a simple program that backs up music and pics from My Documents onto my external. i have the program working more or less. but what i'm looking to do ultimately is making the program look for a previous backup on the external, if there is, delete it and continue backing up the music/pictures. for the copy i've been using shutil.copytree since it copies sub directories. Any advice would be great : )

I've been working on this on my Mac at work, so the source/destination is set on Mac, but i'm going to be using it on my Windows desktop, so any advice i would love it to be focused on windows.

CODE

import os
import sys
from time import sleep
import shutil



def start():
    print 'This is the backup program'
    sleep(2)
    print 'What will you back up?'
    print '1) Pictures'
    print '2) Music'
    print '3) Quit'

    selection = input()
    if selection == (1):
        pictures()
    if selection == (2):
        music()
    if selection == (3):
        exit.sys
    else:
        print 'incorrect selection'
        sleep(2)
        start()

    

def pictures():
    print 'pictures it is'
    sleep(2)
    shutil.copytree('/Users/Minerva/Pictures', '/Users/Minerva/Desktop/Pictures')
    print 'copy completed'
    start()


def Music():
    print 'Music it is'
    sleep(2)
    shutil.copytree('/Users/Minerva/Music', '/Users/Minerva/Desktop/Music')
    print 'copy completed'
    start()

start()



User is offlineProfile CardPM
+Quote Post


girasquid

RE: Python Backup Program

30 Sep, 2009 - 01:47 PM
Post #2

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,648



Thanked: 58 times
Dream Kudos: 825
My Contributions
So...what exactly is the issue here?

Your code has some minor style problems, but seems to run just fine.

In regards to style, you may want to try storing your options as a dict of function references, like this:
CODE

OPTIONS = {
1: pictures,
2: music,
3: exit,
}

...At that point, instead of this:
CODE

    if selection == (1):
        pictures()
    if selection == (2):
        music()

You can just write something like this:
CODE

OPTIONS[selection]()


The other style point I'd like to make concerns reusability; what happens if you one day find you'd like to re-use this code somewhere else? As it stands, you'd have to rewrite it - instead of having your script call it's start() function at the bottom, wrap it like this:
CODE

if __name__ == '__main__':
start()

..That way, if you ever want to use the code in another project, you can do things like from mybackupscript import music, and get access to the functions that you need.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 04:38PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month