From ce7c2102a17054c63f5ff71ed36e5af6476a1aee Mon Sep 17 00:00:00 2001 From: Julius Freudenberger Date: Mon, 1 May 2023 12:03:38 +0200 Subject: [PATCH] Add check to ensure export directory exists --- .gitignore | 1 + csv_exporter.py | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 39d6292..46008f8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ # Edit at https://www.toptal.com/developers/gitignore?templates=python,pycharm urls.txt +export/ ### PyCharm ### # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider diff --git a/csv_exporter.py b/csv_exporter.py index 4419e50..737e870 100644 --- a/csv_exporter.py +++ b/csv_exporter.py @@ -1,6 +1,9 @@ +import os.path + from tripadvisor_attraction import TripadvisorAttraction, TripadvisorReview TABLE_HEADING = "username^review_title^review_text^posting_date^count_stars^count_likes^translated_by" +EXPORT_DIRECTORY = 'export' def export_attraction(attraction: TripadvisorAttraction): @@ -8,7 +11,9 @@ def export_attraction(attraction: TripadvisorAttraction): for review in attraction.reviews: export_string += line_for_review(review) + '\n' - with open(f'export/{attraction.title} ({attraction.count_of_reviews}).csv', 'w') as export_file: + if not os.path.isdir(EXPORT_DIRECTORY): + os.makedirs(EXPORT_DIRECTORY) + with open(f'{EXPORT_DIRECTORY}/{attraction.title} ({attraction.count_of_reviews}).csv', 'w') as export_file: export_file.writelines(export_string)