0

I have a folder with hundreds of .json files. How to open all these files at once?

I have tried:

for i in os.listdir('C:/Users/new'):
    if i.endswith('.json'):
        files.append(open(i).read)
But I got this error:

FileNotFoundError: [Errno 2] No such file or directory:

Mario Cianciolo
  • 1,223
  • 10
  • 17
Acy
  • 13
  • 3

1 Answers1

1

i is only the filename. you should give the full path to the program.

example: let first file be stackoverflow.json

you try to open with filename such as:

open('stackoverflow.json', 'r')

what you should do is:

open('C:/Users/new/stackoverflow.json', 'r')

so the code should do it:

files = []
base_path = 'C:/Users/new'
for i in os.listdir(base_path):
    if i.endswith('.json'):
        full_path = '%s/%s' % (base_path, i)
        files.append(open(full_path, 'r', encoding='utf-8').read())

   print("starting to print json documents...")
   for single_file in files:
      print(single_file)
   print("printing done")

EDIT: as @khelwood states, you should also replace read with read().

Ali Yılmaz
  • 1,657
  • 1
  • 11
  • 28
  • 2
    Consider also [`os.path.join`](https://docs.python.org/2/library/os.path.html#os.path.join) – khelwood May 12 '18 at 15:43
  • @user9781180 that problem seems to be out of context. can you share the full code and example input file? – Ali Yılmaz May 12 '18 at 15:54
  • Sorry. I rerun and it came up with another problem. NameError: name 'files' is not defined – Acy May 12 '18 at 15:56
  • its okay. the error is there because you never declared a variable named `files`. check my answer, i edited the code. – Ali Yılmaz May 12 '18 at 15:58
  • I've tried the newest code, however i got the error : UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 876: character maps to – Acy May 12 '18 at 16:00
  • @user9781180 I updated the last line. can you try it this way? – Ali Yılmaz May 12 '18 at 16:05
  • I tried it. I just have to print(files) for my content to load right? however i got the error of : IOPub data rate exceeded. – Acy May 12 '18 at 16:08
  • @user9781180 can you elaborate? what do you want to do with the data? I update the answer with print statements if you want to print them and see on the terminal logs. – Ali Yılmaz May 12 '18 at 16:10
  • Hi! thankyou so much. It worked! – Acy May 12 '18 at 16:11
  • is it possible to explain the last line of the code on the 'encoding='utf-8'? – Acy May 12 '18 at 16:12
  • @user9781180 in default, python uses utf-8 encoding in order to encode strings, i thought that might be causing your unicode error. i wanted to make sure your configuration is set to default. – Ali Yılmaz May 12 '18 at 16:13
  • I see... last question. What about this line : full_path = '%s/%s' % (base_path, i) – Acy May 12 '18 at 16:15
  • first, I declare the string structure. that is shown by "%s/%s". the "%s" means that a string variable will come there. so, in the structure, i pointed out that I wanted two string variables with a slash (/) between them. after that, I put the strings I want in the right order, which are `base_path` and `i`. – Ali Yılmaz May 12 '18 at 16:16
  • for more info you can refer to https://stackoverflow.com/questions/997797/what-does-s-mean-in-python – Ali Yılmaz May 12 '18 at 16:17
  • Thank you! I'll read more on it. – Acy May 12 '18 at 16:21