简体   繁体   中英

Python - Trying to Save File into Docker Volume

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

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, eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM