Python Backup Program

a simple program that backs up music and pictures to an external drive

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 6320 Views - Last Post: 16 January 2011 - 03:32 PM Rate Topic: -----

#1 gelnika  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 24-September 09

Python Backup Program

Posted 30 September 2009 - 02:31 PM

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.

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()



Is This A Good Question/Topic? 1
  • +

Replies To: Python Backup Program

#2 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Python Backup Program

Posted 30 September 2009 - 02:47 PM

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:
OPTIONS = {
1: pictures,
2: music,
3: exit,
}


...At that point, instead of this:
	if selection == (1):
		pictures()
	if selection == (2):
		music()


You can just write something like this:
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:
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.
Was This Post Helpful? 4

#3 amir-shakh  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 8
  • Joined: 14-January 11

Re: Python Backup Program

Posted 14 January 2011 - 07:16 AM

hi mr ! (Barbarbar)
can u type a comment for each line for this program?
i need it to understand this code
plz help me
bye
Was This Post Helpful? -2
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Python Backup Program

Posted 14 January 2011 - 07:50 AM

girasquid that's a really clever way of doing it. Love it!
Was This Post Helpful? 1
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Python Backup Program

Posted 14 January 2011 - 08:08 AM

If you're going to make a list, you might as well make a loop:
opts = ('Pictures','Music')
optCount = len(opts)
print 'This is the backup program'
hold()
print 'What will you back up?'
for i in range(optCount):
	print (i+1),')', opts[i]
print (optCount+1), ') Quit'



Note, always calling start() is very bad. Put the menu in a loop as well.
Was This Post Helpful? 3
  • +
  • -

#6 LinuxFan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 82
  • Joined: 19-December 10

Re: Python Backup Program

Posted 14 January 2011 - 12:15 PM

Expanding on what baavgai and girasquid said, I saw you wanted to delete existing backups before backing up. Try something like this (using your current imports):
def removeBackup(backup_directory):
    """Removes backup directory. If directory does not exist, returns False. returns True if successful."""
    if os.path.isdir(backup_directory):
        shutil.rmtree(backup_directory)
        return True
    elif os.path.isfile(backup_directory):
        #in case the directory gets corrupt (it happens on *nix sometimes)
        os.remove(backup_directory)
        return True
    else: #Directory/File doesn't exist
        return False


And simply call this method before backing up. You don't have to use the return values; I just wrote it like that for re-usability.

I can understand why you'd want to remove the directory first; If you delete something from the directory you backup and then re-backup, the file would reappear. (I suppose.)

Hope I helped.

This post has been edited by LinuxFan: 14 January 2011 - 12:16 PM

Was This Post Helpful? 2
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Python Backup Program

Posted 14 January 2011 - 12:17 PM

View PostLinuxFan, on 14 January 2011 - 01:15 PM, said:

Expanding on what baavgai and girasquid said, I saw you wanted to delete existing backups before backing up. Try something like this (using your current imports):
def removeBackup(backup_directory):
    """Removes backup directory. If directory does not exist, returns False. returns True if successful."""
    if os.path.isdir(backup_directory):
        shutil.rmtree(backup_directory)
        return True
    elif os.path.isfile(backup_directory):
        #in case the directory gets corrupt (it happens on *nix sometimes)
        os.remove(backup_directory)
        return True
    else: #Directory/File doesn't exist
        return False


You don't have to use the return values; I just wrote it like that for re-usability.

I can understand why you'd want to remove the directory first; If you delete something from the directory you backup and then re-backup, the file would reappear. (I suppose.)

Hope I helped.


Modular and reusable, this is beautiful code LinuxFan. Good thinking!
Was This Post Helpful? 1
  • +
  • -

#8 LinuxFan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 82
  • Joined: 19-December 10

Re: Python Backup Program

Posted 14 January 2011 - 12:23 PM

View Postatraub, on 14 January 2011 - 01:17 PM, said:

<snip quotes>

Modular and reusable, this is beautiful code LinuxFan. Good thinking!

Thanks! I've been trying to learn modularity/re-usability, it's nice to hear praise for that :)
Was This Post Helpful? 1
  • +
  • -

#9 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Python Backup Program

Posted 14 January 2011 - 12:28 PM

I can't believe I overlooked this!!! In Python 2.x 'input' is a huge security flaw.

You should really change:
selection = input()

to
selection = int(raw_input())


otherwise someone could pass in something like "os.remove(somethingImportant)" for selection and cause you some major problems!

This post has been edited by atraub: 14 January 2011 - 12:31 PM
Reason for edit:: dang ol' security flaws man.

Was This Post Helpful? 2
  • +
  • -

#10 LinuxFan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 82
  • Joined: 19-December 10

Re: Python Backup Program

Posted 14 January 2011 - 12:32 PM

View Postatraub, on 14 January 2011 - 01:28 PM, said:

I can't believe I overlooked this!!! In Python 2.x input is a huge security flaw.

You should really change:
selection = input()

to
selection = int(raw_input())


otherwise someone could pass in something like "os.remove(somethingImportant)" for selection and cause you some major problems!

Don't forget to wrap it in try/except blocks:
#in your main loop...
try: 
    selection = int(raw_input())
except ValueError, e:
    print('Invalid selection')
    continue


Was This Post Helpful? 1
  • +
  • -

#11 amir-shakh  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 8
  • Joined: 14-January 11

Re: Python Backup Program

Posted 14 January 2011 - 12:32 PM

anybody can post ,final and last code of above program on the topic

and say how can i run it?
Was This Post Helpful? -3
  • +
  • -

#12 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Python Backup Program

Posted 14 January 2011 - 12:44 PM

I explained this to you earlier Amir:

Quote

first, you have to download Python. www.python.org is a good place to get it. Make sure you get version 2.7 as this code is not Python 3 compliant. Save the program into a .py file and then open a program called IDLE (which will be installed automatically when you download Python). Load the py file into IDLE and then choose run (or hit f5). This is the easiest way for you to run it for the time being.

The program will backup pictures or music; or at least it'll try to. The file path "/Users/Minerva/" is specific to the guy who posted the question. Since you don't have a /Users/Minerva/ directory on your computer, it would just throw you an error when you got to that part.


What part are you having trouble with?

This post has been edited by atraub: 14 January 2011 - 12:44 PM
Reason for edit:: dang ol' question mark

Was This Post Helpful? 0
  • +
  • -

#13 LinuxFan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 82
  • Joined: 19-December 10

Re: Python Backup Program

Posted 14 January 2011 - 12:47 PM

He wants the finished source of the backup program - he doesn't know how to/doesn't want to implement the source including the suggestions.

I've already done that; If it's alright, I'll post it. (Waiting for the OK from atraub or topic starter)

EDIT: Posted the code; it's on the second page.

This post has been edited by LinuxFan: 14 January 2011 - 01:02 PM

Was This Post Helpful? 0
  • +
  • -

#14 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Python Backup Program

Posted 14 January 2011 - 12:47 PM

View Postgirasquid, on 30 September 2009 - 03:47 PM, said:

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.



building on this idea, the style that Guido Van Rossum recommends is

if __name__ == "__main__":
    sys.exit(start())


and have start simply return an int rather than calling sys.exit().
Was This Post Helpful? 2
  • +
  • -

#15 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Python Backup Program

Posted 14 January 2011 - 12:53 PM

Amir wants us to write comments beside every line of code and explain it to him, and he wants us to adjust the code so it'll work on his computer. This is getting very old very fast. Learn to program so you can adjust the code yourself. No one would be doing you any favors by doing all the work for you.

That being said, I don't see a problem with posting the code implementing all of the suggestions we've given thus far.


Edit:
by the way, I can't send you private messages nor can I e-mail you LinuxFan. I'm not saying you have to change the settings, but it would make private communication a bit easier.

This post has been edited by atraub: 14 January 2011 - 12:56 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2