def IncrementString(str_in, inc=1): # The last character of str_in must be in [A-Za-z0-9] # 'Z' wraps to 'A' with a carry to next-to-last character # 'z' wraps to 'z' with a carry to next-to-last character # '9' wraps to '0' with a carry to next-to-last character # So "aaaa" is legal, and "/foo/bar/aaa" is legal, but not "/foo/" # or "/foo/zzz" (which carries) because we can't increment the "/". chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" if str_in == "": raise ValueError("String is not incrmentable: [%s]" % str_in) ch_last = str_in[-1] if ch_last not in chars: raise ValueError("String is not incrmentable: [%s]" % str_in) if ch_last == "z": ret = IncrementString(str_in[:-1]) + 'a' elif ch_last == "Z": ret = IncrementString(str_in[:-1]) + 'A' elif ch_last == "9": ret = IncrementString(str_in[:-1]) + '0' else: ret = str_in[:-1] + chr(ord(ch_last) + 1) return ret # IncrementString