How To/python/MyMiscUtils/

# -*- coding: utf-8 -*-
 
import os,shutil
 
stats = []
outmode = 1 # 0: none, 1: fill stats, 2: fill + print
 
def copypath(source, target, sourceroot = ''):
    """ copy path from source to target """
    """
    use root parameter to specify path to path you want to copy. Eg. you can
    give absolute path or something like that eg.:
    copypath('pics', 'new_dir\\backup', 'c:\\windows\\..\\myfiles\\')
    """
    r = os.path.normpath(sourceroot)
    s = os.path.normpath(source).lstrip('\\').lstrip('/')
    t = os.path.normpath(target)
    # create path
    tp = os.path.join(t, os.path.dirname(s))
    if not os.path.exists(tp):
        os.makedirs(os.path.join(t, os.path.dirname(s)))
    #copy file    
    shutil.copy2(os.path.join(r, s), os.path.join(t, s));
### /copypath()
 
def copyallfiles(source, target):
    """ copy all files from source to target """
    stat = 0
    locstats = stats
 
    if outmode > 0:
        locstats.append('Copy all files from %s' % source)
        if outmode > 1:
            print('Copy all files from %s' % source)
 
    for fname in os.listdir(source):
        ps = os.path.join(source,fname)
        if os.path.isfile(ps):
            if outmode > 0:
                locstats.append('   Copying %s' % fname)
                if outmode > 1:
                    print('   Copying %s' % fname)
            shutil.copy2(os.path.join(source,file), target)
            stat += 1
 
    if outmode > 0:
        locstats.append(' %s file(s) copied.' % stat)
        if outmode > 1:
            print(' %s file(s) copied.' % stat)
### /copyallfiles()
 
def deleteallfiles(source):
    """ delete all files from source """
    stat = 0
    locstats = stats
 
    if outmode > 0:
        locstats.append('Delete all files from %s' % source)
        if outmode > 1:
            print('Delete all files from %s' % source)
 
    for fname in os.listdir(source):
        ps = os.path.join(source,fname)
        if os.path.isfile(ps):
            if outmode > 0:
                locstats.append('   Deleting %s' % fname)
                if outmode > 1:
                    print('   Deleting %s' % fname)
            os.remove(os.path.join(source,file))
            stat += 1
 
    if outmode > 0:
        locstats.append(' %s file(s) deleted.' % stat)
        if outmode > 1:
            print(' %s file(s) deleted.' % stat)
### /deleteallfiles()
 
"""
RecuFilesOperations
 
Reads dir recusively and calls methods:
fileOperations(string currentPath, string currentFile) when file found or
dirOperations(string currentPath, string currentDir) when dir is found.
 
dirOperations(...) method have to return True otherwise content of current
dir will be not read. You may want to use this feature to skip listning of
some dirs.
 
eg. of use:
 
class myMoviesLister(RecuFilesOperations):
    def fileOperation(self, path, fname):
        print 'file %s in path %s' % (fname, path)
 
    def dirOperation(self, path, dirname):
        if dirname == 'porn':
            return False # skip listing this dir!
        else:
            print 'Now listing %s dir' % dirname
            return True
 
myMoviesLister('movies')
"""
 
class RecuFilesOperations:
 
    def __init__(self, path):
        self.recuDirNo = 0
        self.recuFileNo = 0
        self.recuLvl = 0
        self.startpath = os.path.normpath(path)
        self.__recuDirRead(self.startpath, 0)
 
    def __recuDirRead(self, path, lvl):
       for f in os.listdir(path):
            self.recuLvl = lvl
            if os.path.isdir(os.path.join(path, f)):
                self.recuDirNo += 1
                if self.dirOperation(path, f):
                    self.__recuDirRead(os.path.join(path, f), lvl + 1)
            else:
                self.recuFileNo += 1
                self.fileOperation(path, f)
 
    def fileOperation(self, path, f):
        print (self.recuLvl * " ") + f
        return True
 
    def dirOperation(self, path, f):
        print path + "/" + f
        return True
### /RecuFilesOperations()