fill gaps in numbering of files in folder Args: folderPath (str): path to folder to search prefix (str): prefix of files to fill gap Returns: None
(folderPath, prefix)
| 18 | |
| 19 | |
| 20 | def fillGaps(folderPath, prefix): |
| 21 | """fill gaps in numbering of files in folder |
| 22 | Args: |
| 23 | folderPath (str): path to folder to search |
| 24 | prefix (str): prefix of files to fill gap |
| 25 | Returns: |
| 26 | None |
| 27 | """ |
| 28 | fileList = getFilesWithPrefix(folderPath, prefix) # files sorted ascending order |
| 29 | fileRegex = re.compile(prefix + "(\d{1,})(.\w+)") |
| 30 | |
| 31 | start = int( |
| 32 | fileRegex.search(fileList[0]).group(1) |
| 33 | ) # start with the minimum number in list |
| 34 | count = start # count to be incremented during checks for gaps |
| 35 | max_length = len( |
| 36 | fileRegex.search(fileList[-1]).group(1) |
| 37 | ) # max length of largest number, for padding zeros |
| 38 | |
| 39 | for file in fileList: |
| 40 | |
| 41 | mo = fileRegex.search(file) |
| 42 | fileNum = int(mo.group(1)) |
| 43 | |
| 44 | if fileNum != count: |
| 45 | newFileName = ( |
| 46 | prefix |
| 47 | + "0" * (max_length - len(str(fileNum))) |
| 48 | + str(count) |
| 49 | + mo.group(2) |
| 50 | ) |
| 51 | shutil.move(os.path.abspath(file), os.path.abspath(newFileName)) |
| 52 | |
| 53 | count += 1 |
| 54 | |
| 55 | |
| 56 | def insertGaps(folderPath, prefix, index): |
no test coverage detected