Reputation: 10182
I have a script that takes information from an email, downloads the images and necessary text, then creates an HTML file with said info. I would also like to implement the ability to zip the files for simpler distribution. The problem I am running into is that the zip function runs before all of the html files are generated, thus excluding them from the archive. Does anyone have any suggestions as to the best way to ensure that all of the HTML files have been created before creating the zip archive? Thank you!
Sample Code:
for csvFile in csvFiles:
for file in os.listdir('.'):
#do stuff
csv_file = csv.DictReader(open(csvFile))
for line in csv_file:
htmlFile = csvFile[:-4]+'-'+line['sequence']+'.html'
htmlCode = '<html goes here>'
htmlData = open(os.path.join('C:/foo/bar', htmlFile), 'w+')
htmlData.write(htmlCode)
print htmlFile+' Complete'
htmlData.close()
for file in allFiles:
archive = zipfile.ZipFile(csvFile[:-4]+'.zip', mode='a')
archive.write(file)
archive.close()
Upvotes: 0
Views: 103
Reputation: 19973
Python code runs sequentially, so your zip function should be running only after all of your HTML files are generated. The problem you are experiencing is probably in for file in allFiles:
-- where is the code that is finding a list of the files and putting it into the allFiles
variable? If that code is running before your HTML generation, then when it's time to create the zip file, allFiles
will have an out-of-date index of all of the files you want to capture.
Upvotes: 3