1

I have thousands of text files in a directory, and would like to save the content of each file in a list (one big file-list) while maintaining the order of the files as they are in the directory. The texts are not to be concatenated. Please see example of my desired output.

Example=['textual content of file one', 'textual content of file two', 'textual content of file three'.....textual content of file n]

My attempt:

filelist=[file-1,file-2,...file-n]
destinationfile="C:\\XXXXX\\dump_large_json.txt"
with open(destinationfile, 'w',encoding='utf-8') as f:
    for file in filelist:
        with open(file, 'r') as f2:
            h=json.dump(f2,f)

output:

TypeError: Object of type 'TextIOWrapper' is not JSON serializable

Any ideas on how to serially dump text files in json format would be appreciated.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
user2274879
  • 349
  • 1
  • 5
  • 16
  • Follow a similar procedure outlined, you need to read from `f`. – cs95 Nov 08 '17 at 18:03
  • Potential duplicate of https://stackoverflow.com/questions/10872604/json-dump-throwing-typeerror-is-not-json-serializable-on-seemingly-vali – Sharad Nov 08 '17 at 18:03
  • It is not a duplicate question, the texts are not to be concatenated as in the solutions you guys pointed out. – user2274879 Nov 08 '17 at 18:20
  • @cᴏʟᴅsᴘᴇᴇᴅ, it is not a duplicate question, the solution you pointed me to involves concatenating the file contexts in one large file which is not what I want. The output file should be a list that contains the textual content of each file. – user2274879 Nov 08 '17 at 18:24

1 Answers1

1

You need to append the text contents of each file to a list, and then dump the resulting object to a json file:

filelist = [file-1, file-2, ... file-n]
destinationfile = "C:\\XXXXX\\dump_large_json.txt"

contents = []
for file in filelist:
    with open(file, 'r') as f:
        contents.append(f.read())

with open(destinationfile, 'w', encoding='utf-8') as f:
    json.dump(contents, f)
ekhumoro
  • 115,249
  • 20
  • 229
  • 336