# Write a program that finds all files with a given prefix, such as spam001.txt,
# spam002.txt, and so on, in a single folder and locates any gaps in the numbering
# (such as if there is a spam001.txt and spam003.txt but no spam002.txt).
#
# Have the program rename all the later files to close this gap.

@@ -298,8 +298,1711 @@
 is gap.%0A
+%0Aimport os, re%0A%0AseqRegex = re.compile(r%22%22%22%0A                      %5E(.*?)    # All text before numbering%0A                      (%5Cd+)     # One or more numbers%0A                      (.*?)$    # All text after numbering%0A                      %22%22%22, re.VERBOSE)%0A%0A%0Adef is_sequence(numberlist):%0A    return len(numberlist) == (numberlist%5B-1%5D - numberlist%5B0%5D + 1)%0A%0A%0Adef fill_gaps(folder):%0A    # Get list of numbers used in file names%0A    numlist = %5B%5D%0A    for foldername, subfolders, filenames in os.walk(folder):%0A        filenames.sort()%0A        for filename in filenames:%0A            match_object = seqRegex.search(filename)%0A%0A            # Skip files without numbering%0A            if match_object is None:%0A                continue%0A%0A            # Get numbers%0A            number = match_object.group(2)%0A            numlist.append(int(number))%0A%0A    # Check if numbers are in sequence%0A    if is_sequence(numlist):%0A        print(%22There are no gaps to fill.%22)%0A        return None%0A%0A    # TODO: Determine missing number in sequence%0A    sequence = list(range(numlist%5B0%5D, numlist%5B-1%5D + 1))%0A%0A    # TODO: Rename folders%0A    for foldername, subfolders, filenames in os.walk(folder):%0A        filenames.sort()%0A        for filename in filenames:%0A            match_object = seqRegex.search(filename)%0A%0A            # Skip files without numbering%0A            if match_object is None:%0A                continue%0A%0A            # Get parts of filename%0A            prefix = match_object.group(1)%0A            number = match_object.group(2)%0A            suffix = match_object.group(3)%0A%0A%0Adef main():%0A    fill_gaps(%22./testdir%22)%0A    return None%0A%0A%0A# If P03_fillGaps.py is run instead of imported, run main():%0Aif __name__ == %22__main__%22:%0A    main()%0A

Update P03_fillGaps Check if files are in sequence