-1

I have a JSON file called data.json and I am trying to print the data inside that JSON file. The JSON file got created by the command:

git log --pretty="format:{"commit":"%h", "merge":"%p", "author":"%an", "title":"%s", "body":"%b"}",>"C:\test_temp\data.json"

I am trying to print the data inside the file with the function parse_json but I am getting an error that says IOError: [Errno 22] invalid mode ('r') or filename "C:\test_temp\data.json"

json_directory = "C:\test_temp\data.json"
def parse_json_file(json_directory):
    with open(json_directory) as f:
        data = json.load(f)
    print(data)

The json file is already there but I am not sure why it cannot read that file. Also the data that got generate from the JSON file does not have proper formatting as the dictionary is not surrounded by the " " even though I indicated it in the executed git log command. Will that cause a problem if I try to parse the json file.

Ashish101
  • 135
  • 10
  • Does this answer your question? [IOError: \[Errno 22\] invalid mode ('r') or filename: 'c:\\Python27\test.txt'](https://stackoverflow.com/questions/15598160/ioerror-errno-22-invalid-mode-r-or-filename-c-python27-test-txt) – Ekrem Dinçel Dec 29 '20 at 21:29

2 Answers2

0

Maybe try:

json_directory = "C:\\test_temp\\data.json" 

KJDII
  • 851
  • 4
  • 11
0

Your command is producing invalid json, so your json.load method call will never succeed.

You need to escape the quotes-- what you have supplied (as you can see from stack overflow's syntax highlighting) is actually a series of strings which your shell is concatenating together.

In BASH on OSX, escaping the strings looks like:

 git log --pretty="format:{\"commit\":\"%h\", \"merge\":\"%p\", \"author\":\"%an\", \"title\":\"%s\", \"body\":\"%b\"}"

You could also enclose the entire argument to pretty with single quotes, as follows:

 git log --pretty='format:{"commit":"%h", "merge":"%p", "author":"%an", "title":"%s", "body":"%b"}',>"C:\test_temp\data.json"

Once your json generation command is corrected, I suspect your script will succeed so long as the paths are correct.

If you try to correct the command as I have recommended and it does not work, please post the JSON file you are generating, as well as the shell you are using.

Maus
  • 1,791
  • 1
  • 17
  • 28
  • I am using windows cmd shell. I used this escape \ method so that the quotes gets printed but I still got the same output as before i.e no quotes – Ashish101 Dec 30 '20 at 02:18
  • if you get no quotes, then the quotes are not being escaped. That's your issue. Maybe try the other form I recommended? – Maus Dec 30 '20 at 19:03