I've been trying to solve this issue, but right now I need some help. I'm trying to upload this JSON file (DBL) in Spyder IDE. I have stored the JSON-data file and the Spyder file, in the same map in order to read the JSON file, but it's not working.
My Python code:
import json
file = open("dbl")
dbl = json.load(file)
print(dbl)
Every time I upload the json file, in the same map as the spyder.py file, it can't recognize the file directory.
I have stored the my .py file in the same folder as the JSON file.
This is the error message:
FileNotFoundError: [Errno 2] No such file or directory: 'dbl.json'
The file, in fact, does not exist. The actual filename is dbl.json.json.
import json
file = open("dbl.json.json")
dbl = json.load(file)
print(dbl)
You'll use ".json" in the file path.
For example:
file = open("dbl.json")
if dbl is name of json file then you should add ".json" extension too. you might do this:
# Opening JSON file
f = open('dbl.json',)
# returns JSON object as
# a dictionary
data = json.load(f)
# Iterating through the json
# list
for i in data:
print(i)
# Closing file
f.close()```
The code is fine, however, it's good practice to add a file extension. Looks like you forgot to add the extension.
You are using relative paths. It is advised to use absolute paths. In this case, put the python script and db1 file in the same directory and try again.
In case, you want to debug just add the below code on top of your script to see if the file is present or not and modify the script accordingly.
import os;
print(os.listdir())
Related
I don't know what's wrong here, all I'm trying to do is to open this file, but it says it can't find such a file or directory, however as I have highlighted on the side, the file is right there. I just want to open it. I have opened files before but never encountered this. I must have missed something, I checked online, and seems like my syntax is correct, but I don't know.
I get the same error when I try with "alphabetical_words" which is just a text file.
When open() function receives a relative path, it looks for that file relative to the current working directory. In other words: relative to the current directory from where the script is run. This can be any arbitrary location.
I guess what you want to do is look for alphabetical.csv file relative to the script location. To do that use the following formula:
from pathlib import Path
# Get directory of this script
THIS_DIR = Path(__file__).absolute().parent
# Get path of CSV file relative to the directory of this script
CSV_PATH = THIS_DIR.joinpath("alphabetical.csv")
# Open the CSV file using a with-block
with CSV_PATH.open(encoding="utf-8") as csvfile:
pass # Do stuff with opened file
You need to import csv. Then you can open the file as a csv file. For example,
with open ("alphabetical.csv", "r") as csvfile:
Then you can use the file.
Make sure the file is in your cwd.
I need to read a CSV file from a folder, which is generating from another Module. If that Module fails it won't generate the folder which will have a CSV file.
Example:
path = 'c/files' --- fixed path
When Module successfully runs it will create a folder called output and a file in it.
path =
'c/files/output/somename.csv'
But here is a catch everytime it generates a output folder, CSV file has a different name.
First i need to check if that output folder and a CSV file is there or not.
After that I need to read that CSV.
The following will check for existance of output folder as well as csv file and read the csv file:
import os
import pandas as pd
if 'output' in os.listdir('c/files'):
if len(os.listdir('c/files/output')>0:
x=[i for i in os.listdir('c/files/output') if i[-3:]=='csv][0]
new_file=pd.read_csv(x)
glob.glob can help.
glob.glob('c/files/output/*.csv') returns either an empty list or a list with (hopefully) the path to a single file
You may also try to get the latest file based on creation time, after you have done check on existence of a directory (from above post). Something like below
list_of_files = glob.glob("c/files/*.csv")
latest_file = max(list_of_files, key=os.path.getctime)
latest_file is the latest created file in your directory.
I am trying to read in a JSON file called league.json from a python file called run_test.py. The JSON file is in a different directory. I am running Windows 10 and python 3.8.6 in Visual Studio.
The directory I'm running the python file in is: C:\Users\my_un\Documents\folder1\folder2\model\run_test.py
The directory I am trying to load the json file from is:
C:\Users\my_un\Documents\folder1\folder2\config\league.json
Based on documentation I've found, the below code should work, but I keep getting the error: FileNotFoundError: [Errno 2] No such file or directory: 'config\league.json'.
This does run successfully when I move league.json to the same directory as the python file, and change the open location to: '.\league.json'.
What am I doing wrong? My code is below. Thanks in advance.
import json
with open('.\config\league.json', 'r') as f:
config = json.load(f)
print (len(config))
change the path to open('..\config\league.json', 'r') , by adding one more .
if the above doesn't work, use abspath to find the path of league.json and pass in the open(path) function
The folder 'config' needs to be in the folder 'model' for your code to run. Otherwise change your code to
open('C:\Users\my_un\Documents\folder1\folder2\config\league.json', 'r')
I have project in Pycharm, and I want to use json file that placed in this project.
How can I call it?
I use this one:
import json
file = open('~/PycharmProjects/Test/JSON_new.json')
x = json.load(file)
And received the following error:
FileNotFoundError: [Errno 2] No such file or directory: '~/PycharmProjects/Test/JSON_new.json'
But path is correct
EDIT: I understood what is the problem. Instead of json file txt was created (but I selected json). It creates txt files, maybe, someone knows hot to solve it? I can create only .py files directly. Other files no.
Is it correct if I create scratch json file and placed it in Scratches?
You may like to use following path(In linux):
file = open('/home/<user>/PycharmProjects/Test/JSON_new.json')
Replace user with your username. You need to know the correct path to the file, for which you can user PWD command in terminal.
You can use json module for this. You can open the file in a seperate object and pass it to json.load if you have a JSON string use json.loads.
import json
file = open('/path/to/json/file.json')
file_opened = json.load(file)
So I have a folder with about 500 JSON files. I need to upload all of them to a local mongodb database. I tried using Mongo Compass, but Compass can only upload one file at a time. In python I tried to write some simple code to iterate through the folder and upload them one by one, but I ran into some problems. First of all the JSON files are not comma-separated, rather line separated. So the files look like:
{ some JSON object }
{ some JSON object }
...
I wrote the following code to iterate through the folder and upload it:
import pymongo
import json
import pandas as pd
import numpy as np
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient['Test']
mycol = mydb['data']
directory = os.fsencode("C:/Users/PB/Desktop/test/")
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith(".json"):
mycol.insert_many(filename)
The code basically goes through a folder, checks if it's a .json file, then inserts it into the database. That is what should happen. However, I get this error:
TypeError: document must be an instance of dict, bson.son.SON,
bson.raw_bson.RawBSONDocument, or a type that inherits from
collections.MutableMapping
I cannot seem to upload it through python. I tried multiple variations of the code, but for some reason the python does not accept the json files.
The problem with these files seems to be that python only allows for comma-separated JSON files.
How could I fix this to upload all the files?
You're inserting the names of the files to mongo. Not the contents of the file.
Assuming you have multiple json files in a directory, where each file contains a json-object in each line...
You need to go through all the files, filter them, open them, read them line by line, parse each line into a dict, and then insert. Something like below:
os.chdir(directory)
for file in os.listdir(directory):
if file.endswith(".json"):
with open(file) as f:
for line in f:
mongo_obj = json.loads(line)
mycol.insert(mongo_obj)
I did a chdir first to avoid having to pass the whole path to open