def file_move(src, dst): if src==dst: return if not exists(src): raise Exception('src not exists') spth, sname=splitdir(src) if sname=='': spth, sname=splitdir(spth) if sname=='': raise Exception('bad src') dpth, dname=splitdir(dst) if dname=='': dpth, dname=splitdir(dpth) if spth==dpth: # print 'rename:', sname, dname os.rename(src, dst) else: cp(src, dst) rm(src)
def indexfiles(rootdir,outputfilepath): totalfiles = 0 outputfilename = "_" + rootdir.replace(':','').replace('\\','.') + ".txt" with codecs.open(join(outputfilepath,outputfilename), "w", encoding='utf8') as output: filetypes = [] #files = [] for (dirname, subdirlist, filelist) in walk(rootdir): subdirlist.sort() filelist.sort() for filename in filelist: ext = splitext(filename)[1] if ext not in [".ini", ".db", ".txt", ".py", ".pdf", ".png", ".jpg"]: if ext not in filetypes: filetypes.append(ext) totalfiles += 1 if dirname == rootdir: output.write(filename + "\n") else: output.write(join(splitdir(dirname)[1], filename) + "\n") #files.append(join(dirname[12:], filename)) print("Number of files found: %s" % totalfiles) print("Observed filetypes: %s" % filetypes) print("Directory listings written to: \"" + outputfilename + "\" in directory " + outputfilepath) return join(outputfilepath, outputfilename)
def indexfiles(rootdir, outputfilepath): totalfiles = 0 outputfilename = "_" + rootdir.replace(':', '').replace('\\', '.') + ".txt" with codecs.open(join(outputfilepath, outputfilename), "w", encoding='utf8') as output: filetypes = [] #files = [] for (dirname, subdirlist, filelist) in walk(rootdir): subdirlist.sort() filelist.sort() for filename in filelist: ext = splitext(filename)[1] if ext not in [ ".ini", ".db", ".txt", ".py", ".pdf", ".png", ".jpg" ]: if ext not in filetypes: filetypes.append(ext) totalfiles += 1 if dirname == rootdir: output.write(filename + "\n") else: output.write( join(splitdir(dirname)[1], filename) + "\n") #files.append(join(dirname[12:], filename)) print("Number of files found: %s" % totalfiles) print("Observed filetypes: %s" % filetypes) print("Directory listings written to: \"" + outputfilename + "\" in directory " + outputfilepath) return join(outputfilepath, outputfilename)
def file_copy(src, dst): if src==dst: return if not exists(src): raise Exception('src not exists') if isfile(src): if isfile(dst): # print 'override file:', src, '->', dst # print '.', shutil.copy2(src, dst) return elif isdir(dst): # print 'copy file into dir:', src, '->', dst # print '.', shutil.copy2(src, dst) return elif not exists(dst): # print 'copy file:', src, '->', dst # print '.', shutil.copy2(src, dst) return else: raise Exception('bad dst') # TODO: maybe dst is link if not isdir(src) or src=='/': raise Exception('bad src') # TODO: maybe dst is link spth, sname=splitdir(src) if sname=='': spth, sname=splitdir(spth) if sname=='': raise Exception('bad sname') if not exists(dst): # print 'makedirs:', dst # print '@', os.makedirs(dst) elif isdir(dst): dst=join(dst, sname) if not exists(dst): # print 'mkdir:', dst # print '#', os.mkdir(dst) elif isdir(dst): # print 'dir exists:', dst pass else: raise Exception('bad dst2') else: raise Exception('src is dir but dst is file or link') for fp in listall(src): cp(fp, dst)
def split_path(fullpath): ''' split a path into 5 parts: fullpath, dirpath, filename, name, ext usage: fp, dp, fn, name, ext=split(fp) ''' dirpath, filename=splitdir(fullpath) name, ext=splitext(filename) return fullpath, dirpath, filename, name, ext
def split(fp): dp, fn=splitdir(fp); name, ext=splitext(fn); return fp, dp, fn, name, ext try: import cjson; jencode=cjson.encode; jdecode=cjson.decode
import os, sys, re, shutil, getopt, datetime, time, urllib, urllib2, struct, logging from os import listdir, getcwd as pwd, chdir as cd, mkdir as md, makedirs as mds, remove as rm, rmdir as rd, system as run from os.path import split as splitdir, splitext, join, dirname, isfile, islink, isdir, exists, abspath, getatime as fatime, getmtime as fmtime, getctime as fctime, getsize as fsize from re import compile, sub as rsub from urlparse import urlsplit, urlunsplit, urlunparse, urljoin, urldefrag from random import sample as rsample, shuffle, randint from copy import deepcopy as clone from pprint import pprint as dump def cerr(s): sys.stderr.write(str(s)+'\n') def cout(s): sys.stdout.write(str(s)+'\n') clog=cerr from sys import argv, exit argc=len(argv) app=splitdir(argv[0])[1] from time import sleep def now(): return datetime.datetime.now() def nowfn(): return str(now()).replace(' ', '_').replace('.', '_').replace(':', '-') def listall(root='.'): return [join(root, i) for i in listdir(root)] def listfiles(root='.'): return filter(isfile, listall(root)) def listdirs(root='.'): return filter(isdir, listall(root)) def listlinks(root='.'): return filter(islink, listall(root)) def u8(s): return s.encode('utf8') if type(s) is unicode else s.decode('utf8') def gb(s): return s.encode('gbk') if type(s) is unicode else s.decode('gbk') def fread(path, binary=False): mode='r' if binary: mode+='b'
def split(fp): dp, fn = splitdir(fp) name, ext = splitext(fn) return fp, dp, fn, name, ext
from pprint import pprint as dump def cerr(s): sys.stderr.write(str(s) + '\n') def cout(s): sys.stdout.write(str(s) + '\n') clog = cerr from sys import argv, exit argc = len(argv) app = splitdir(argv[0])[1] from time import sleep def now(): return datetime.datetime.now() def nowfn(): return str(now()).replace(' ', '_').replace('.', '_').replace(':', '-') def listall(root='.'): return [join(root, i) for i in listdir(root)]
file_out.write("==== Added files ====\n") for line in addedfiles: file_out.write("[ADD]**" + line) file_out.write("\n\n====Removed files====\n") for line in removedfiles: file_out.write("[DEL]**" + line) print( "\n\nA total of %s differences were found: %s new files and %s removed files." % (len(addedfiles) + len(removedfiles), len(addedfiles), len(removedfiles))) print("Differences written to: \"" + difffile + "\" in " + outdir) [currentdir, currentfile] = splitdir(realpath(__file__)) if currentfile == "_movie_merge.py": rootdirs = [Movies, currentdir] elif currentfile == "_show_merge.py": rootdirs = [TVShows, currentdir] else: print(currentfile + " in " + currentdir) print("\nScanning originals directory") origional = indexfiles(rootdirs[0], outdir) print("\n\nScanning backup directory") backup = indexfiles(rootdirs[1], outdir)
def split(fullpath): dirpath, filename=splitdir(fullpath) name, ext=splitext(filename) return fullpath, dirpath, filename, name, ext
# If either of the directories weren't read in, then quit print("orig: %s, bak: %s" % (orgdir, bakdir)) if orgdir == "" or bakdir == "": print("error: Bad logfile") quit() with codecs.open(join(indir,"_log.txt"), 'w', encoding='utf8') as log: log.write("Original directory: " + orgdir + "\n") log.write("Backup directory : " + bakdir + "\n\n") for line in diff: if line.startswith("[ADD]"): line = line.replace("\n","").split("**") src = join(orgdir,line[1]) dst = join(bakdir,line[1]) if not isdir(splitdir(dst)[0]): print("Directory \'" + splitdir(dst)[0] + "\' does not exist. Creating directory.") log.write("Directory \'" + splitdir(dst)[0] + "\' does not exist. Creating directory.\n") mkdir(splitdir(dst)[0]) try: print("Copying " + src + " to " + dst + "") log.write("Copying " + src + " to " + dst + "\n") copy2(src, dst) except: print("error: %s not copied" % join(orgdir,line[1])) log.write("error: " + join(orgdir,line[1]) + " not copied\n") elif line.startswith("[DEL]"): line = line.replace("\n","").split("**") dst = join(bakdir,line[1]) try:
file_out.write("[ORG_DIR]**" + rootdirs[0] + "\n") file_out.write("[BAK_DIR]**" + rootdirs[1] + "\n\n\n") file_out.write("==== Added files ====\n") for line in addedfiles: file_out.write("[ADD]**" + line) file_out.write("\n\n====Removed files====\n") for line in removedfiles: file_out.write("[DEL]**" + line) print("\n\nA total of %s differences were found: %s new files and %s removed files." % (len(addedfiles)+len(removedfiles),len(addedfiles),len(removedfiles))) print("Differences written to: \"" + difffile + "\" in " + outdir) [currentdir, currentfile] = splitdir(realpath(__file__)) if currentfile == "_movie_merge.py": rootdirs = [Movies,currentdir] elif currentfile == "_show_merge.py": rootdirs = [TVShows,currentdir] else: print(currentfile + " in " + currentdir) print("\nScanning originals directory") origional = indexfiles(rootdirs[0],outdir) print("\n\nScanning backup directory")
# If either of the directories weren't read in, then quit print("orig: %s, bak: %s" % (orgdir, bakdir)) if orgdir == "" or bakdir == "": print("error: Bad logfile") quit() with codecs.open(join(indir, "_log.txt"), 'w', encoding='utf8') as log: log.write("Original directory: " + orgdir + "\n") log.write("Backup directory : " + bakdir + "\n\n") for line in diff: if line.startswith("[ADD]"): line = line.replace("\n", "").split("**") src = join(orgdir, line[1]) dst = join(bakdir, line[1]) if not isdir(splitdir(dst)[0]): print("Directory \'" + splitdir(dst)[0] + "\' does not exist. Creating directory.") log.write("Directory \'" + splitdir(dst)[0] + "\' does not exist. Creating directory.\n") mkdir(splitdir(dst)[0]) try: print("Copying " + src + " to " + dst + "") log.write("Copying " + src + " to " + dst + "\n") copy2(src, dst) except: print("error: %s not copied" % join(orgdir, line[1])) log.write("error: " + join(orgdir, line[1]) + " not copied\n") elif line.startswith("[DEL]"):