You could get a list of all filenames that match the pattern. Extract the last components as numbers, and add 1 to the maximum.
i = 1 + max(int(name.split('.')[-1])
for name in glob.glob('filename.0.0.*))
That assumes that there already is at least one such file and all such files have a last component that can be parsed as an int. Take an appropriate amount of care.
Or you could also create a file, say lastname.0.0.31, to track the name, and when you find it there, create filename.0.0.32 and replace lastname.0.0.32; panic if there is more than one lastname.0.0.*, or
fewer than one.
Or as above but track with nextname.0.0.31 to create filename.0.0.31 and replace the tracking name with nextname.0.0.32 for the next file.
Or save the number somewhere else.