0

I am getting this error while trying to import:

Traceback (most recent call last):
  File "C:\PythonSC\module21.py", line 8, in <module>
    page = open('c:\restaurants\restaurants.json','r')
OSError: [Errno 22] Invalid argument: 'c:\restaurants\restaurants.json'

here is my source code of trying to import:

import json
import pymongo
from pymongo import MongoClient

client = MongoClient('localhost', 27017)
db = client.restaurants
collection = db.restaurantsCollection
page = open('c:\restaurants\restaurants.json','r')
dataset = json.loads(page.read())

for item in dataset:
    collection.insert(item)

for item in collection.find():
    print(item)

and here is the sample json data from https://raw.githubusercontent.com/mongodb/docs-assets/geospatial/restaurants.json

I still could not understand why this error and I hope someone could lend me a helping hand. Thanks a lot.

1
  • 1
    'c:\restaurants\restaurants.json' -> r'c:\restaurants\restaurants.json'? It's possible it wants to read backslashes as escape characters. r makes the string "raw", so you don't have to escape backslashes themselves. Always provide regexes and paths as raw strings. And passwords, if entered in the code or the console if entered as a string. Commented Sep 17, 2019 at 14:30

1 Answer 1

1

1.Place your json file in your project repository and then use it for reading.

f= open("[file_name]","r+")

2.Other way(Second Method) is to use os module to get position of file and use it as path=os.getcwd()+[append your file name] open(path,'r+');

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.