import sys,os,glob
import datetime
import zipfile
def translatedate(date):
#returns a datetime.date object from a string representing a date
def errorprompt(msg):
#user-display of errors, prompts for retry or quit
def getTCOM(date,store):
os.chdir(r"\\PATH\TO\ARCHIVE")
filename = "{:04}{:02}{:02}.ZIP".format(store,date.month,date.day)
#the day's paperwork that I'm looking for
coms_filter = ["coms{:02}{:02}{}.zip".format(date.month,date.day,
str(date.year)[-2:]) for\
date in (date+datetime.timedelta(day) for\
day in range(0,11))]
#all the archives I feel may contain that paperwork date
zipfiles = [zipfile.ZipFile(path) for path in coms_filter if\
path in glob.glob("coms*.zip")]
#a list of those archives (as ZipFiles) that actually exist
output = None
for archive in zipfiles:
if not output:
file = [file for file in archive.infolist() if file.filename.split("/")[-1] == filename]
if file:
tmp = archive.extract(file[0],path=".")
output = True
archive.close()
#iterate through and find the paperwork date I'm looking for, closing every
#archive I opened earlier. This can (and will) be refactored so I pass it a
#path and use a context manager to open each one and search but speed
#doesn't matter to me yet -- just want function
return tmp #return the location of the paperwork date file I extracted
def main():
date = None
while date is None:
datestring = input("Paperwork date (mm/dd/yyyy): ")
try: date = translatedate(datestring) #get the date, there's
except ValueError as e: #some error checking that
errorprompt(e) #goes along in this process
except:
raise #uncaught errors get raised
store = None #same for store, error chk etc
storelist = [a_list_of_our_store_numbers]
while store is None:
try: store = int(input("Store number: "))
except TypeError:
errorprompt("That was not a number")
if store not in storelist:
store = None
errorprompt("Invalid Store Number")
tcom_file = getTCOM(date,store) #gets the path of the com file I need
tcom = zipfile.ZipFile(tcom_file)#opens it as a ZipFile
#do useful stuff here
tcom.close() #closes it
os.remove(tcom_file) #deletes it since I still have the archive
if __name__ == "__main__":
main()