mercredi 10 novembre 2021

how to find the required pattern for a file using regex in python?

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.pdf
  • 22-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))

Aucun commentaire:

Enregistrer un commentaire