Reputation: 478
im very new in python programming, so i created the following python script in order to iterate in a folder which contain 3000 or more json files, and i dont know how to iterate in those 3000 json files to do not put manually in the script, and i need to put a specific path where are the json files located as well and i dont know how to declare it.
import json
all_results = {}
json_file_list = ['1.json', '2.json']
for file in json_file_list:
with open(file) as json_file:
json_data = json.load(json_file)
for key, value in json_data.iteritems():
if 'result' in value:
all_results[key] = value['result']
return all_results
after run my python script i have the following error :
File "getResult.py", line 20
return all_results
SyntaxError: 'return' outside function
and this is how looks like my jsons files
{ "igt@gem_reloc_overflow@single-overflow": { "type": "TestResult", "command": "/home/gfx/intel-graphics/intel-gpu-tools/tests/gem_reloc_overflow --run-subtest single-overflow", "dmesg": "", "environment": "PIGLIT_PLATFORM=\"mixed_glx_egl\" PIGLIT_SOURCE_DIR=\"/home/gfx/intel-graphics/intel-gpu-tools/piglit\"", "err": "(gem_reloc_overflow:19562) CRITICAL: Test assertion failure function reloc_tests, file gem_reloc_overflow.c:260:\n(gem_reloc_overflow:19562) CRITICAL: Failed assertion: gem_execbuf(fd, &execbuf) == -14\n(gem_reloc_overflow:19562) CRITICAL: error: -22 != -14\nSubtest single-overflow failed.\n**** DEBUG ****\n(gem_reloc_overflow:19562) DEBUG: relocation_count=4294967295\n(gem_reloc_overflow:19562) CRITICAL: Test assertion failure function reloc_tests, file gem_reloc_overflow.c:260:\n(gem_reloc_overflow:19562) CRITICAL: Failed assertion: __gem_execbuf(fd, &execbuf) == -14\n(gem_reloc_overflow:19562) CRITICAL: error: -22 != -14\n**** END ****\n", "exception": null, "out": "IGT-Version: 1.14-g1e9a3ac (x86_64) (Linux: 4.6.0-rc4-drm-intel-nightly-ww17-commit-1e81bac+ x86_64)\nStack trace:\n #0 [__igt_fail_assert+0x101]\n #1 [reloc_tests+0x6d6]\n #2 [+0x6d6]\nSubtest single-overflow: FAIL (8.469s)\n", "pid": 19562, "result": "fail", "returncode": 99, "subtests": { "__type": "Subtests" }, "time": { "type": "TimeAttribute", "end": 1462072402.5360818, "start": 1462072393.7328644 }, "traceback": null } }
any help is welcome, thanks
Upvotes: 0
Views: 54
Reputation: 1563
Store the path of JSON file directory to a variable
mypath = "/home/json_file_directory"
Use os.listdir() to list all the json files.
Once you have json filename,use
os.path.join(mypath,filename) #to get the exact path of file.
Upvotes: 0
Reputation: 13016
To iterate over the files in a directory without having to enter the filenames manually, you can use os.walk()
or os.listdir()
. See this question for more details.
The syntax error seems pretty self-explanatory. You have a return
statement that is not inside of a function definition; this is invalid Python. If you want to output the results, use print
.
Upvotes: 0
Reputation: 249153
Perhaps you want print all_results
instead of return all_results
. Or you could print them one by one as they are found.
Upvotes: 0