Skip to content

Instantly share code, notes, and snippets.

@eli2and40
Created August 3, 2020 14:27
Show Gist options
  • Save eli2and40/365ca429eea3c6772e4ac2346dda00cd to your computer and use it in GitHub Desktop.
Save eli2and40/365ca429eea3c6772e4ac2346dda00cd to your computer and use it in GitHub Desktop.
"""
This little piggy helps you avoid accidentally overwriting downloads/auto-generated files
"""
def nameUpdater(path):
"""
Returns a unique version of a given string by appending an integer
Dependencies: os module
In: string
Out: string_2
"""
import os
id = 2
oldPath = path[:]
# while path == oldPath: ##for test use
while os.path.exists(path):
newPath = list(os.path.splitext(path))
if '_' in newPath[0]:
if newPath[0].split('_')[-1].isnumeric():
# print('case1a')
id = newPath[0].split('_')[-1]
newPath[0] = newPath[0].replace(f'_{id}', f'_{str(int(id)+1)}')
path = ''.join(newPath)
else:
# print('case1b')
newPath[0] += f'_{id}'
path = ''.join(newPath)
id += 1
else:
# print('case2')
newPath[0] += f'_{id}'
path = ''.join(newPath)
id += 1
return path
nameSpacer = nameUpdater
@gh-doot
Copy link

gh-doot commented Aug 10, 2020

nice bro!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment