MCPcopy Create free account
hub / github.com/Mrinank-Bhowmick/python-beginner-projects / fillGaps

Function fillGaps

projects/fill-gaps/fill_gaps.py:20–53  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

18
19
20def 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
56def insertGaps(folderPath, prefix, index):

Callers 1

fill_gaps.pyFile · 0.85

Calls 3

getFilesWithPrefixFunction · 0.85
searchMethod · 0.80
moveMethod · 0.45

Tested by

no test coverage detected