Skip to content

Instantly share code, notes, and snippets.

@kurzweil777
Last active July 25, 2020 13:14
Show Gist options
  • Save kurzweil777/d7145e6362d7162ce395c103f1833327 to your computer and use it in GitHub Desktop.
Save kurzweil777/d7145e6362d7162ce395c103f1833327 to your computer and use it in GitHub Desktop.
Exercise from automate boring stuff
#!python3
import os
import re
"""As an added challenge, write another program that can insert gaps into numbered files so that a new file can be
added. """
working_directory = input("Input your working directory with files: ")
numbers = [[correct_number + 1, number_of_file] for foldername, subfolder, filename in
os.walk(working_directory) for correct_number, number_of_file in enumerate(filename)]
regex = re.compile(r'''[0-9]{1,10}''')
for number, filename in numbers:
result = regex.findall(str(filename))
try:
if str(''.join(result)) == str(number):
os.rename(working_directory + f"\\{filename}",
working_directory + f"\\{number+1}.JPG")
except FileExistsError:
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment