Earth & Dusk org

Combined Output :

def increment_filename(filename):
"""
If a file exists, add a number to the filename to make it unique.
Example: if test.txt exists, return test(1).txt
"""
if not os.path.exists(filename):
return filename

directory = os.path.dirname(filename)
name, ext = os.path.splitext(os.path.basename(filename))
counter = 1

while True:
    new_name = os.path.join(directory, f"{name}({counter}){ext}")
    if not os.path.exists(new_name):
        return new_name
    counter += 1
Duskfallcrew changed pull request status to merged
Your need to confirm your account before you can post a new comment.

Sign up or log in to comment