Unable to use(file manipulation) the Pyline --output-format:file

37 Views Asked by At

Using pylint in a python script as,

--output-format=json:report.json

The warning from Pylint are stored in the report.json file. But its not possible to read / json.dump the report.json file.

For example, after successfully execute the Run() from pylint.lint, its not possible to do,

with open(report.json, "r", encoding='utf-8') as file:
    pylint_data = file.read()
    - or - 
    pylint_data = json.load(file)

I presume the Pylint process kept the file busy or not allow to read, but what is the reason?

Ok, as someone asked for a minimal reproducible example, here it is.

from pylint.lint import Run
import os
import json

DEFAULT_SCRIPT_DIRECTORIES = ["scripts/"]

PYLINT_JSON_FILE = 'pylint_output.json'
PYLINT_CONFIG = "pylintrc"

cmd = [f'--rcfile={PYLINT_CONFIG}']
cmd += [f'--output-format=json:{PYLINT_JSON_FILE}']

for script_path in DEFAULT_SCRIPT_DIRECTORIES:
    add_path = os.path.abspath(script_path)
    if os.path.isfile(add_path):
        add_path = os.path.dirname(add_path)
    cmd.append(add_path)

pylint_run = Run(cmd, do_exit=False)

if os.path.exists(PYLINT_JSON_FILE):
    with open(PYLINT_JSON_FILE, "r", encoding='utf-8') as file:
        pylint_data = json.load(file)
else:
    print("Failed to create PyLint JSON report.")

in the json.load() I'm getting, json.decoder.JSONDecodeError Error. I've checked manually, the PYLINT_JSON_FILE exists with valid data.

0

There are 0 best solutions below