Getting TypeError: 'NoneType' object is not iterable while reading a JSON file to store the values in a list

Question:

Even though the variable is having the 1st dictionary value for the key name from the JSON file, while storing that value to a list I am getting output as None. This seems to be a strange.. Am I missing something. Could you please help

Code:

import os
import shutil
import json

MainDir = "F:/Protocols/20180207-Iteration/"
os.chdir(MainDir)
MainDir = os.getcwd()
# print(MainDir)

#Load the JSON data to a file
with open("F:/Protocols/20180207-Iteration/rout.json",'r',encoding='utf-8') as f:
    json_file = json.load(f)

#To avoid appending delete the file all_iteration.txt if it exists
if os.path.exists("all_iteration.txt"):
    os.remove("all_iteration.txt")

#Create all_iteration.txt which will store the foldernames of the particular Iteration inside the Iteration folder
Path_List = []
for values in json_file['value']:
    Folder_Names = values['name']
    Indivisuals = str(Folder_Names)
    Path_List = list(Path_List.append(Indivisuals))
    print(Path_List)
print(Path_List)

Log:

E:python.exe "C:Program FilesJetBrainsPyCharm Community Edition 2017.3.3helperspydevpydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 51533 --file C:/Users/SPAR/PycharmProjects/Sample/PATH_FINDER.py
pydev debugger: process 10528 is connecting

Connected to pydev debugger (build 173.4301.16)
Traceback (most recent call last):
  File "C:Program FilesJetBrainsPyCharm Community Edition 2017.3.3helperspydevpydevd.py", line 1668, in <module>
    main()
  File "C:Program FilesJetBrainsPyCharm Community Edition 2017.3.3helperspydevpydevd.py", line 1662, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:Program FilesJetBrainsPyCharm Community Edition 2017.3.3helperspydevpydevd.py", line 1072, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:Program FilesJetBrainsPyCharm Community Edition 2017.3.3helperspydev_pydev_imps_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"n", file, 'exec'), glob, loc)
  File "C:/Users/SPAR/PycharmProjects/Sample/PATH_FINDER.py", line 23, in <module>
    Path_List = list(Path_List.append(Indivisuals))
TypeError: 'NoneType' object is not iterable

Process finished with exit code 1
Asked By: suraj das

||

Answers:

list.append() method doesn’t return anything – it just appends the item into the list. Hence, you’re trying to do list(None), which is raising the error since you can’t create a list out of a NoneType object.

Just change the line

Path_List = list(Path_List.append(Indivisuals))

to

Path_List.append(Indivisuals)
Answered By: TerryA
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.