Add check to ensure export directory exists

This commit is contained in:
Julius Freudenberger 2023-05-01 12:03:38 +02:00
parent d5081f6cd8
commit ce7c2102a1
2 changed files with 7 additions and 1 deletions

1
.gitignore vendored
View file

@ -2,6 +2,7 @@
# Edit at https://www.toptal.com/developers/gitignore?templates=python,pycharm # Edit at https://www.toptal.com/developers/gitignore?templates=python,pycharm
urls.txt urls.txt
export/
### PyCharm ### ### PyCharm ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider

View file

@ -1,6 +1,9 @@
import os.path
from tripadvisor_attraction import TripadvisorAttraction, TripadvisorReview from tripadvisor_attraction import TripadvisorAttraction, TripadvisorReview
TABLE_HEADING = "username^review_title^review_text^posting_date^count_stars^count_likes^translated_by" TABLE_HEADING = "username^review_title^review_text^posting_date^count_stars^count_likes^translated_by"
EXPORT_DIRECTORY = 'export'
def export_attraction(attraction: TripadvisorAttraction): def export_attraction(attraction: TripadvisorAttraction):
@ -8,7 +11,9 @@ def export_attraction(attraction: TripadvisorAttraction):
for review in attraction.reviews: for review in attraction.reviews:
export_string += line_for_review(review) + '\n' 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) export_file.writelines(export_string)