2

I'm trying to write a python file in json within a Docker container. I'm using Jupyter Notebook in the container to load some urls from the NBA API, and eventually I want to write it to a NoSql database somewhere but for now I want the files to be saved inside the volume of the Docker container.. Unfortunately, when I run this Python code, I get the error

FileNotFoundError: [Errno 2] No such file or directory: '/usr/src/app/nba-matches/0022101210.json'

Here is the Python code... I'm running

from run import *
save_nba_matches(['0022101210'])

on Jupyter notebook.

The Python script:

import os
import urllib.request
import json
import logging

BASE_URL = 'https://cdn.nba.com/static/json/liveData/playbyplay/playbyplay_'
PATH = os.path.join(os.getcwd(), 'nba-matches')
LOGGER = logging.getLogger()
LOGGER.setLevel(logging.INFO)


def save_nba_matches(match_ids):
    for match_id in match_ids:
        match_url = BASE_URL + match_id + '.json'
        json_file = os.path.join(PATH, match_id+'.json')

        web_url = urllib.request.urlopen(
            match_url)
        data = web_url.read()
        encoding = web_url.info().get_content_charset('utf-8')
        json_object = json.loads(data.decode(encoding))
        with open(json_file, "w+") as f:
            json.dump(json_object, f)
        logging.info(
            'JSON dumped into path: [' + json_file + ']')

Dockerfile:

FROM python:3

WORKDIR /usr/src/app

COPY requirements.txt ./

RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["jupyter", "notebook", "--port=8888", "--no-browser", "--ip=0.0.0.0", "--allow-root"]

and finally, docker-compose.yml

version: '3.4'
services:
  sports-app:
    build: .
    volumes:
      - '.:/usr/src/app'
    ports:
      - 8888:8888
1
  • Does the nba-matches directory exist in the mounted host directory? You'll get that error if it doesn't. (Consider mounting only that directory in the Compose setup so that the application code in the image isn't overwritten by the bind mount.) Commented Apr 10, 2022 at 1:17

1 Answer 1

1

Based on the error look's you don't have the directory called “nba-matches” and the function save_nba_matches needs it; You could add directory validation, e.g.

os.makedirs(PATH, exist_ok=True)

run.py

import os
import urllib.request
import json
import logging

BASE_URL = 'https://cdn.nba.com/static/json/liveData/playbyplay/playbyplay_'
PATH = os.path.join(os.getcwd(), 'nba-matches')
LOGGER = logging.getLogger()
LOGGER.setLevel(logging.INFO)


def save_nba_matches(match_ids):
    for match_id in match_ids:
        match_url = BASE_URL + match_id + '.json'
        json_file = os.path.join(PATH, match_id+'.json')
        web_url = urllib.request.urlopen(
            match_url)
        data = web_url.read()
        encoding = web_url.info().get_content_charset('utf-8')
        json_object = json.loads(data.decode(encoding))
        os.makedirs(PATH, exist_ok=True)
        with open(json_file, "w+") as f:
            json.dump(json_object, f)
        logging.info(
            'JSON dumped into path: [' + json_file + ']')

or just manually creating the directory nba-matches it will work

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.