To look for a static pattern, you don’t really need regular expressions.
As an aside, I would change your script to simply look in the current directory; then it’s easy to test on a set of files you want to test on without having to experiment with the real production location.
# from os.path import isfile <- unused, comment out import os import shutil destination = "n:/Test/" f_start = int(input('select starting number: ')) f_end = int(input('select ending number: ')) for file in os.listdir("."): if "_REC-" in file and f_start <= int(file.split("-")) <= f_end: shutil.copy(file, destination)
If you really wanted to use regular expressions, maybe something along the lines of
import re pattern = re.compile(r"\d+_REC-(\d+)-\d+\.PDF") ... matching = pattern.match(file) if matching and f_start <= int(matching.group(1)) <= f_end:
The regex imposes a stricter check on whether the file name matches the expected pattern (where of course I had to guess what exactly you expect). If you have files which contain
_REC- but don’t adhere to the pattern, maybe a regex would be useful. But for simple requirements, it’s easier to see what the code does if you just pick apart the file name enough to extract the parts you need, and then you don’t have to learn to read regex just yet. (For example, if it’s not quite strict enough, maybe also check if the file name starts with a number and ends with
Notice also how we need to convert strings containing numbers to
int in order to perform numeric comparisons. (If you forget,
"123" will seem to be “smaller” than
1 comes before
CLICK HERE to find out more related problems solutions.