I tried to match the pattern of a file in my folders the file extension is a pdf.
I have many pdf files that have the same pattern but with different name at the end.
the pattern includes date + name of the file.
The problem is that when I run the script the system consider the both file name as the first pattern (python_pt) and do not go for the elif statement.
Example:
10-11-2021 python.pdf22-09-2021 java.pdf
Code:
import re
import os
from os import path
from tqdm import tqdm
from time import sleep
python_pt= "^[0-3]?[0-9]-[0-3]?[0-9]-(?:[0-9]{2})?[0-9]{2}$ python.pdf"
java_pt1= "^[0-3]?[0-9]-[0-3]?[0-9]-(?:[0-9]{2})?[0-9]{2}$ java.pdf"
java_pt2= "^ java [0-3]?[0-9]-[0-3]?[0-9]-(?:[0-9]{2})?[0-9]{2}$.pdf"
str = 'c:'
a = 0
i = 0
for dirpath, dirnames, files in os.walk(src, topdown=True):
print(f'\nFound directory: {dirpath}\n')
for file in tqdm(files):
sleep(.1)
full_file_name = os.path.join(dirpath, file)
if os.path.join(dirpath) == src:
if file.endswith("pdf"):
if python_pt:
i+=1
elif java_pt1 or java_pt2:
a+=1
print("{} file 1 \n".format(i))
print("{} file 2 \n".format(a))
src? Also you are misusing anchors, none of those patterns really work as$marks the end of string, and you require some more chars after that. And you never use the patterns, as to run a regex check, you need to usere.match/re.search/re.fullmatch. Please make sure you try these with updated patterns (without random use of anchors) and if you still fail, please edit the question.