Skip to content

Commit

Permalink
use context manager to open file
Browse files Browse the repository at this point in the history
  • Loading branch information
jswilson committed Aug 25, 2020
1 parent 5447751 commit 869b215
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,16 +383,15 @@ def write_subset_of_urls_to_sitemap(self, filename, index):
# index: zero-based index from which to start writing url strings contained in
# self.url_strings_to_output
try:
sitemap_file = open(filename, 'w')
with open(filename, 'w') as sitemap_file:
start_index = index
end_index = (index + self.MAX_URLS_PER_SITEMAP)
sitemap_url_strings = self.url_strings_to_output[start_index:end_index]
self.write_sitemap_file(sitemap_file, sitemap_url_strings)
except:
logging.error("Could not open sitemap file that is part of index.")
exit(255)

start_index = index
end_index = (index + self.MAX_URLS_PER_SITEMAP)
sitemap_url_strings = self.url_strings_to_output[start_index:end_index]
self.write_sitemap_file(sitemap_file, sitemap_url_strings)

@staticmethod
def write_sitemap_file(file, url_strings):
print(config.xml_header, file=file)
Expand Down

0 comments on commit 869b215

Please sign in to comment.