Reputation: 942
This is my example path: 'c:\Data\2015-08-01'
Currently I'm getting all the files inside on one(1) specific date, but my goal is to get the files with date range of file folder. Example is to get 2015-08-01 to 2015-08-05' just like the BETWEEN
query in MySQL
import os
import os.path
import tempfile
dateStart = '2015-08-01'
dateEnd = '2015-08-05'
year = dateStart[0:4]
yearMonth = year + '_' + dateStart[5:7]
pathDir = 'c:\\Data'
date_folder = pathDir + '\\' + dateStart
count = 0
for filefolder in os.listdir(date_folder):
filefolder = date_folder + "\\" + filefolder
for file in os.listdir(filefolder):
if "txt" in file:
filename = filefolder + "\\" + file
print filename
#Output of this, is all text files for this date only '2015-08-01'
Its hard for me to loop to pull files for date range e.g. '2015-08-01' to '2015-08-05'. How to do this?
Note that there is a folder after my dates and the textfiles are in the last. and the textfile containing on that folder is my point to get. so that from my old code I used this: filefolder = date_folder + "\" + filefolder to get the text in 1 date only.
Here is my sample real path data:
\\10.81.67.162\DLCx Logs\DLCx02\2015\2015_08\2015-08-01\Folder\data.text
and if I will get the range from 2015-08-01 to 2015-08-01. this will be the output:
\\10.81.67.162\DLCx Logs\DLCx02\2015\2015_08\2015-08-01\Folder\data.text
\\10.81.67.162\DLCx Logs\DLCx02\2015\2015_08\2015-08-02\Folder\data.text
\\10.81.67.162\DLCx Logs\DLCx02\2015\2015_08\2015-08-03\Folder\data.text
\\10.81.67.162\DLCx Logs\DLCx02\2015\2015_08\2015-08-04\Folder\data.text
\\10.81.67.162\DLCx Logs\DLCx02\2015\2015_08\2015-08-05\Folder\data.text
Upvotes: 4
Views: 9999
Reputation: 123423
The datetime
module makes doing date arithmetic, comparisons, as well as converting them to or from strings relatively easy.
Here's how it could be used to do what you're trying to accomplish (at least according to your most recent comments):
from datetime import datetime, timedelta
from glob import glob
from os import path
DATE_FORMAT = '%Y-%m-%d'
SUBFOLDER_PATH_FORMAT = r'%Y\%Y_%m\%Y-%m-%d\Folder'
pathDir = r'\\10.81.67.162\DLCx Logs\DLCx02'
dateStart = '2015-08-01'
dateEnd = '2015-09-01'
start_date = datetime.strptime(dateStart, DATE_FORMAT).date()
end_date = datetime.strptime(dateEnd, DATE_FORMAT).date()
delta_one_day = timedelta(days=1)
date = start_date
while date <= end_date:
subfolder_path = date.strftime(SUBFOLDER_PATH_FORMAT)
data_folder = path.join(pathDir, subfolder_path)
if path.isdir(data_folder):
for filename in glob(os.path.join(data_folder, '*.txt')):
print filename
date += delta_one_day
Upvotes: 1
Reputation: 40688
Here is my approach: start with separate year, month, day and build the date:
import glob
import os
pattern = os.path.join(r'C:\Data', '{}-{:02}-{:02}', '*', '*.txt')
year, month = 2015, 8
start_day, end_day = 1, 5
for day in range(start_day, end_day + 1):
wildcard = pattern.format(year, month, day)
for filename in glob.glob(wildcard):
print filename
Upvotes: 3
Reputation: 1970
It is easiest to convert your dates to date objects. You can then just compare them. See example below:
#!/usr/bin/python
import os
import os.path
import tempfile
import datetime
import re
dateStart = '2015-08-03'
dateEnd = '2015-08-05'
# helper function to convert date strings to date objects
def make_dt(ds):
return datetime.date(int(ds[0:4]), int(ds[5:7]), int(ds[8:10]))
# convert date string to date object
dt_start = make_dt(dateStart)
dt_end = make_dt(dateEnd)
pathDir = '.'
if __name__ == "__main__":
for folder in os.listdir(pathDir):
# only folders that match date format yyyy-mm-dd
if re.match("[0-9]{4}-[0-9]{2}-[0-9]{2}", folder):
# convert folder name to date object
dt_folder = make_dt(folder)
if (dt_folder <= dt_end) and (dt_folder >= dt_start):
print "folder %s is between start [%s] and end [%s]" % (folder, dateStart, dateEnd)
Upvotes: 0