Skip to content

Instantly share code, notes, and snippets.

@Pullusb
Last active July 19, 2016 10:15
Show Gist options
  • Save Pullusb/db6566875491ad78dab4d4c6821999bc to your computer and use it in GitHub Desktop.
Save Pullusb/db6566875491ad78dab4d4c6821999bc to your computer and use it in GitHub Desktop.
python function, append name to version number in given path with regex (look for version format like '_V02_' or '.v02' )
def regplace(text, add, c=None):
'''
Get a path and return it with string appended :
Take a path containing a version number as first argument (parsed with the regex compiled passed as optional third arg)
Return it with string (given as second argument) suffixed to version
'''
if not c:
c = re.compile('([\.,_])([v,V]\d{2,3})([\.,_]?)')
head, tail = os.path.split (text)
splitpath = [head,tail]
newSplitPath = []
for p in splitpath:
num = c.search(p)
if p and num:
try:
if num.group(3): #if trailing separator exist replace it with preceding and place trailing after the new text
new = c.sub(num.group(1) + num.group(2) + num.group(1) + add + num.group(3), p)
else: #if no trailing separator, simply place preceding as trailing and append new text
new = c.sub(num.group(0) + num.group(1) + add, p)
newSplitPath.append(new)
except:
print ("could not find version in path:", p)
else:
newSplitPath.append(p)
newPath = (os.path.join(newSplitPath[0], newSplitPath[1]))
return (newPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment