def makeLinks(fileGlobPattern, filePathRegex, subTemplate, outputFolder, counters = None , hardLink=False): """ Globs all files with the pattern fileGlobPattern and builds for each found file a new symlink where the file name is determined by match/substituting the filePath with the regex: filePathRegex and substitution pattern: subTemplate which produces the new file name counter is a list of tuples each consisting of (start, key lambda) which defines the start of a counter and a sort lambda which takes a filepath and returns a sort key for the sort function. all associated counters are then replaced (formatted) in the new file name , substituted by the subTemplate. Then, it saves a new symlink in the outputFolder for each of these new file names. Example: makeSymlinkSequence( "./*.xml", r".*?/Data-(\d*)-(\d*).jpg", "NewFileLink-\1-\2", "./output/files") """ reg = re.compile(filePathRegex) files = glob2.glob(fileGlobPattern); def matchAndRepl(f): m=reg.match(f) if m: return (f, m.expand(subTemplate),m.groups()) else: return None files = [ matchAndRepl(f) for f in files] files = filter(lambda f: f is not None, files) # standart counter (sort paths) if counters is None: counters = [(0,lambda path,regexG: os.path.basename(path))] for counter in counters: count = counter[0] # the start of this counter # sort files according to counter sort lambda files = sorted(files, key = lambda f: counter[1]( f[0] , f[2]) ) # assign counter for i,f in enumerate(files): files[i] = f + (count,) count += 1 os.makedirs(outputFolder,exist_ok=True); print("Make symlinks in folder: ", outputFolder) # make symlinks to all files in outputFolder for tu in files: #list [ (file,newfile, regexGroups, counter1, counter1,..., counterN) , .... ] f = tu[0] newf = tu[1] if newf is None: continue # skip this file as regex did not match! simLinkFile = newf.format(*tu[3:]); simLinkPath = os.path.join(outputFolder,simLinkFile) CE.printInfo("Link: " + f + " --- to ---> " + simLinkPath) try: if hardLink: os.link(os.path.abspath(f), simLinkPath ); else: os.symlink(os.path.abspath(f), simLinkPath ); except FileExistsError: CE.printInfo("file exists, continue")
def makeDirectory(path,interact=True, name="Directory", defaultCreate=True, defaultMakeEmpty=False): if not os.path.exists(path) : CE.printInfo("%s : %s" % (name,path) + " does not exist!") default = 'y' if defaultCreate else 'n' r='' if interact: r = input(CE.makePrompt("Do you want to create directory [y|n, default(%s)]: " % default)).strip() r = r or default if r == 'n': raise CE.MyValueError("Aborted due to not created directory!") else: os.makedirs(path) CE.printInfo("Created %s : %s" % (name,path)) else: mess = "%s : %s" % (name,path) + " does already exist!" if interact: CE.printWarning(mess) else: CE.printInfo(mess) # dir exists default='y' if defaultMakeEmpty else 'n' r='' if interact: r = input(CE.makePrompt("Do you want to remove and recreate the directory [y|n, default(%s)]: " % default) ).strip() r = r or default if r == 'y': shutil.rmtree(path) os.makedirs(path) CE.printInfo("Removed and created %s : %s" % (name,path))