-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilingTheGaps.py
More file actions
23 lines (19 loc) · 866 Bytes
/
filingTheGaps.py
File metadata and controls
23 lines (19 loc) · 866 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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
import shutil, os, re
def fillInTheGaps(folder):
folder = os.path.abspath(folder)
errorRegex = re.compile(r'(\D*)(\d+)(.*)')
for foldername, subfolder, files in os.walk(folder):
for file in files:
mo = errorRegex.search(os.path.join(foldername, file))
if mo == None:
continue
else:
beforePart = mo.group(1)
numberPart = mo.group(2)
afterPart = mo.group(3)
print(numberPart.lstrip('00'))
fillInTheGaps('C:\\eggs')