Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chp7: extra else statement? #501

Closed
xyang2013 opened this issue Jan 22, 2025 · 1 comment · Fixed by #504
Closed

Chp7: extra else statement? #501

xyang2013 opened this issue Jan 22, 2025 · 1 comment · Fixed by #504
Assignees
Labels
question Further information is requested

Comments

@xyang2013
Copy link

import json
import os
import urllib


def download_and_load_file(file_path, url):

    if not os.path.exists(file_path):
        with urllib.request.urlopen(url) as response:
            text_data = response.read().decode("utf-8")
        with open(file_path, "w", encoding="utf-8") as file:
            file.write(text_data)
    else:
        with open(file_path, "r", encoding="utf-8") as file:
            text_data = file.read()

    with open(file_path, "r", encoding="utf-8") as file:
        data = json.load(file)

    return data


file_path = "instruction-data.json"
url = (
    "https://raw.githubusercontent.com/rasbt/LLMs-from-scratch"
    "/main/ch07/01_main-chapter-code/instruction-data.json"
)

data = download_and_load_file(file_path, url)
print("Number of entries:", len(data))

I think the else statement is not required:

    else:
        with open(file_path, "r", encoding="utf-8") as file:
            text_data = file.read()
@xyang2013 xyang2013 added the question Further information is requested label Jan 22, 2025
@rasbt
Copy link
Owner

rasbt commented Jan 23, 2025

Good catch, you are absolutely right. It's redundant here. I am removing it via #504

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants