python increment filename by 1

Solutions on MaxInterview for python increment filename by 1 by the best coders in the world

showing results for - "python increment filename by 1"
Virgil
07 Jan 2020
1import glob
2import re
3import os
4
5
6def main():
7    # We want all files that are S00E0x
8	for name in glob.glob('./*S0*'):
9        # We want to find in the filename 'Exx' and increase xx by 1
10		new_name = re.sub('(E)([0-9]{2})', increment, name)
11		os.rename(name, new_name)
12
13
14def increment(num):
15    # Return the first match which is 'E'. Return the 2nd match + 1 which is 'x + 1'
16	return num.group(1) + str(int(num.group(2)) + 1).zfill(2)
17
18
19if __name__ == '__main__':
20	main()
21