Created
August 3, 2020 14:27
-
-
Save eli2and40/365ca429eea3c6772e4ac2346dda00cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice bro!