0

I have some code that saves a json file and prints it to screen. I am trying to find the best way to iterate through a directory of files, printing one file after another, but I am receiving an '[Errno 13] Permission Denied' error.

At present I am doing the following:

json_path = 'MYPATH'
json_files = [f for f in os.listdir(json_path) if f.endswith('.json')]

for jf in json_files:
    with open (os.path.join(json_path)) as my_jf:
        json_text = json.load(my_jf)
        print(json_text)

I have made sure that the folder in the path is not opened elsewhere, and I have access to it. If there is a simpler way to achieve this I would appreciate the input.

cookie1986
  • 865
  • 12
  • 27
  • https://stackoverflow.com/questions/10575750/python-ioerror-errno-13-permission-denied this could help you – Sundeep Pidugu Oct 15 '19 at 14:00
  • make sure to close the file after saving it. Perhaps it wasn't close after save and you are opening it again for reading and printing to console. – NeverHopeless Oct 15 '19 at 14:05

3 Answers3

0

You are not really open the files, you are opening the path where the files are located. You could try to change:

with open (os.path.join(json_files)) as my_jf:
Renan Lopes
  • 1,229
  • 2
  • 10
  • 17
0

I have stumbled across an answer of sorts. If I create a list of the text files in the directory the json.load request seems to work:

my files = ['file1.txt', 'file2.txt']

for file in myfiles:
    with open(file) as json_file:
    jsonconvo = json.load(json_file)
    print(jsonconvo)

I'm unsure if I've necessarily overcome the the actual issue, but this seems like a reasonable workaround.

cookie1986
  • 865
  • 12
  • 27
0

Looks like you just forgot to actually include the file name in your open() statement:

with open(os.path.join(json_path, jf)) as my_jf:
glibdud
  • 7,550
  • 4
  • 27
  • 37