Question : Using a variable to match in a regular expression

Hello, I'm a bit confused with the regular expression search function in a project i'm completing. The program requires a "number of days" input from a user via a tkinter input box which then needs to be used in order to find zip files with a corresponding timestamp in a directory. The matching files are then unzipped for each date between now and then.

Basically, I'm having trouble with the matching of the regular expression to the timestamp portion of the zip filename:

if re.search('inputdatef+', fl)

An example of a filename is PUBLIC_TRADINGIS_200810110000_0000000212109527 where "200810110000" is the timestamp.  I have tried assigning the variable 'inputdatef' to match to the current filename in the loop but it doesn't seem to be matching any files.

Thanks for any suggestions in advance.

Paul
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
def finddays(days):
 
    print "Obtain data for the previous " + days + " days"
 
    #unzip contents of 'zip' folder
 
 
    zipcount = 0
    days = int(days)
 
    for fl in sorted(listdir(rawdataDir)):                  #iterate through each file in raw directory 
 
        e = datetime.now()-timedelta(days=days)             #set the day to import using the users input (shuffles forward a day in each loop)
        inputdatef = e.strftime("%Y%m%d%H%M")               #convert date to a format similar to filename
        
        if re.search('inputdatef+', fl):                    #only unzip file if the current file in loop is after the date specified by user
            zipcount = zipcount + 1                         #counter
            z = zipfile.ZipFile(rawdataDir + fl, 'r')       #create zipfile module object
            for name in z.namelist():                       #loop through all files in zip archive 
                if name.find('.csv')!= 1:                   #only extract files ending in .csv
                    z.extract(name, extractdataDir)         #extract current file in loop to tmp directory
            z.close()
 
        days = days - 1                                     #shuffle forward a day for next iteration of loop 
            
    print 'unzipped ' + str(zipcount) + ' files successfully'
Open in New Window Select All

Answer : Using a variable to match in a regular expression

"""
How doesn't the decrementing of days not look right? It needs to look for one day less than the day specified by the user until the last file created (which would be the most recent file created "today").
"""

That would be fine if you weren't decrementing days inside a loop over filenames. Under very specific circumstances what you've done may work, but more than likely you'll end up skipping filenames that match. Consider the following,

$ ls
PUBLIC_TRADINGIS_200810120000.zip
PUBLIC_TRADINGIS_200810100000.zip  PUBLIC_TRADINGIS_200810130000.zip
PUBLIC_TRADINGIS_200810110000.zip  PUBLIC_TRADINGIS_200810140000.zip

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
import os
from datetime import datetime, timedelta
now = datetime(2008, 10, 14, 00, 00, 00)
 
def yours(days):
    for fl in sorted(os.listdir('.')):
        e = now - timedelta(days=days)
        inputdatef = e.strftime("%Y%m%d%H%M")
 
        if inputdatef in fl:
            print fl
 
        days = days - 1
 
def another(days):
    files = sorted(os.listdir('.'))
 
    for fl in sorted(os.listdir('.')):
        for day in range(1, days+1):
            e = now - timedelta(days=day)
            inputdatef = e.strftime("%Y%m%d%H%M")
 
            if inputdatef in fl:
                print fl
 
print '# yours'
yours(2)
print '# another'
another(2)
 
--- 
 
# yours
# another
PUBLIC_TRADINGIS_200810120000.zip
PUBLIC_TRADINGIS_200810130000.zip
 
---
 
BTW: you may want to take a closer look at your use of strftime. In your code the timestamp you create will always use the hour and minute that the script is executed (from datetime.now()) which I presume is not what you intended. 
Open in New Window Select All
Random Solutions  
 
programming4us programming4us