files,folders,paths

ecu_jon hayesjdno3 at yahoo.com
Tue Feb 15 04:55:47 CET 2011


well i think i will close this out. redid recursive foldering now for
3rd or 4th time. below script works from a windows to another folder
(destination1) or a unc path(destination2). i tried it on my linux
box, and the error deals with writing to a remote smb share. i think
if it was mounted somewhere, it would work fine with something like
dest1, but changing the path(or mounting the remote drive to backup2
for the lazy ).

#!/usr/bin/python
import wx,os,string,fnmatch,shutil,tempfile,time,getpass
from datetime import *
from os.path import join, getsize


def weekChoice():
    #week = 0
    dateNum = datetime.now().day
    if dateNum <=7:
        week = 1
    elif (dateNum >= 8) and (dateNum <= 14):
        week = 2
    elif (dateNum >= 15) and (dateNum <= 21):
        week = 3
    elif dateNum > 22:
        week = 4
    else:
        print "error!!!!"
    return week

def getusername():
    username = getpass.getuser()
    return username

def homeDir():
    homedir = os.path.expanduser('~')
    try:
        from win32com.shell import shellcon, shell
        homedir = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0,
0)
    except ImportError:
        homedir = os.path.expanduser("~")
    return homedir

def source1():
    week = weekChoice()
    homedir = homeDir()
    #changed backupdir to source
    source = os.path.join(homedir, "backup")
    return source

def destination1():
    week = weekChoice()
    homedir = homeDir()
    remotedir = os.path.join(homedir, "backup2")
    weekstr = "week"+str(week)
    destination = os.path.join(remotedir, weekstr)
    return destination

def destination2():
    username = getusername()
    week = weekChoice()
    weekstr = "week"+str(week)
    destination = os.path.join("//Mothera",username,weekstr)  #need to
mount the smb share somewhere
    return destination

def backupall():
    week = weekChoice()
    weekstr = "week"+str(week)
    source = source1()
    destination = destination2() # change this around to suit needs
    for (path, dirs, files) in os.walk(source):
        for fname in dirs:
            leftover = path.replace(source, '')
            #print "leftover = ",leftover
            currentdir = destination+leftover
            #print "currentdir = ",currentdir
            try:
                #print "dest folder to
make",os.path.join(currentdir,fname)
                os.mkdir(os.path.join(currentdir,fname),0755)
            except:
                print "error folder"
        for fname in files:
            leftover = path.replace(source, '')
            #print "leftover = ",leftover
            currentdir = destination+leftover
            #print "currentdir = ",currentdir
            try:
                #print "dest file to
make",os.path.join(currentdir,fname)
 
shutil.copy2(os.path.join(path,fname),os.path.join(currentdir,fname))
            except:
                print "error file"
backupall()



More information about the Python-list mailing list