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)
Related
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())
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 been trying to read this csv file using pandas and then transfer it to a dictionary using:
pandas.read_csv("/Users/vijayaswani/Downloads/England1\ postcodes.csv ", index_col=1).T.to_dict()
but each time I get the error No such file or directory
neither does using the name of the file work and nor does using its path even though the file is not deleted or anything.
What could be the issue?
Looks like you have an extra space in the file path.
Have you tried:
pandas.read_csv("/Users/vijayaswani/Downloads/England1\ postcodes.csv", index_col=1).T.to_dict()
Or
pandas.read_csv("/Users/vijayaswani/Downloads/England1 postcodes.csv", index_col=1).T.to_dict()
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
I'm currently working on a Python Flask API.
For demo purposes, I have a folder in the server containing .tar.gz files.
Basically I'm wondering how do I save these files knowing their relative path name, say like file.tar.gz, into a FILE object. I need the tar file in the format to be able to run the following code on it, where f would be the tar file:
tar = tarfile.open(mode="r:gz", fileobj=f)
for member in tar.getnames():
tf = tar.extractfile(member)
Thanks in advance!
Not ver familiar with this but , just saving it normally with .tar.gz extension should work? if yes and if you have the file already compressed then a very simple code could do that,
compresseddata= 'your file'
with open('file.tar.gz') as fo:
fo.write(compressed data)
fo.flush().close()
Will this do the job , or am i getting something wrong here?