Here's my code:
import json
with open("json.items") as json_file:
json_data = json.load(json_file)
It works fine when I move the json file into the same directory. However, I'm trying to get the json file from a different directory. How would I do that? This is what I have tried and its not working:
with open("/lowerfolder/json.items") as json_file:
Any help? Thanks
Depending on your platform, starting a path with / means absolute path from the root
Meaning a relative path should be open("lowerfolder/json.items") without the /
Related
I am trying to open a CSV file that I recently saved using Python. Here is global directory:
So the folder is called Parsing Data Using Python, and there are 3 files inside, we only concern ourselves with the codealong.py file, which is some Python code that I want to run to open the 'patrons.csv' file.
Here is the code in the codealong.py file:
import csv
html_output = ''
with open('patrons.csv','r') as data_file:
csv_data = csv.reader(data_file)
print(list(csv_data))
When I run this, I get the Error2: No such file or directory: 'patrons.csv'
Any ideas why I am getting this? Because I am saving patrons.csv in the same directory as codealong.py I thought the file would be detected!
One approach would be to set the working directory to be the same location as where your script is. To do this for any script, add this to the top:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
This takes the full name of where your script is located, takes just the path element and sets the current working directory to that.
You then would not need to specify the full path to your file.
Another approach using pathlib.
import csv
from pathlib import Path
file = Path(__file__).parent / "patrons.csv"
with file.open("r", encoding="utf-8") as data_file:
csv_data = csv.reader(data_file)
print(list(csv_data))
This seems like it should be simple enough, but haven't been able to find a working example of how to approach this. Simply put I am generating a JSON file based on a list that a script generates. What I would like to do, is use some variables to run the dump() function, and produce a json file into specific folders. By default it of course dumps into the same place the .py file is located, but can't seem to find a way to run the .py file separately, and then produce the JSON file in a new folder of my choice:
import json
name = 'Best'
season = '2019-2020'
blah = ['steve','martin']
with open(season + '.json', 'w') as json_file:
json.dump(blah, json_file)
Take for example the above. What I'd want to do is the following:
Take the variable 'name', and use that to generate a folder of the same name inside the folder the .py file is itself. This would then place the JSON file, in the folder, that I can then manipulate.
Right now my issue is that I can't find a way to produce the file in a specific folder. Any suggestions, as this does seem simple enough, but nothing I've found had a method to do this. Thanks!
Python's pathlib is quite convenient to use for this task:
import json
from pathlib import Path
data = ['steve','martin']
season = '2019-2020'
Paths of the new directory and json file:
base = Path('Best')
jsonpath = base / (season + ".json")
Create the directory if it does not exist and write json file:
base.mkdir(exist_ok=True)
jsonpath.write_text(json.dumps(data))
This will create the directory relative to the directory you started the script in. If you wanted a absolute path, you could use Path('/somewhere/Best').
If you wanted to start the script while beeing in some other directory and still create the new directory into the script's directory, use: Path(__file__).resolve().parent / 'Best'.
First of all, instead of doing everything in same place have a separate function to create folder (if already not present) and dump json data as below:
def write_json(target_path, target_file, data):
if not os.path.exists(target_path):
try:
os.makedirs(target_path)
except Exception as e:
print(e)
raise
with open(os.path.join(target_path, target_file), 'w') as f:
json.dump(data, f)
Then call your function like :
write_json('/usr/home/target', 'my_json.json', my_json_data)
Use string format
import json
import os
name = 'Best'
season = '2019-2020'
blah = ['steve','martin']
try:
os.mkdir(name)
except OSError as error:
print(error)
with open("{}/{}.json".format(name,season), 'w') as json_file:
json.dump(blah, json_file)
Use os.path.join():
with open(os.path.join(name, season+'.json'), 'w') as json_file
The advantage above writing a literal slash is that it will automatically pick the type of slash for the operating system you are on (slash on linux, backslash on windows)
I need to export my pandas dataframe as a csv in to the folder. My python solution is deployed on the IIS Server, where i get the "file_path" and the url, i use these two things to fetch or read my file.
This is what i did to read the incoming file.
from urllib.parse import quote
url = "http://20.9.6.11:8066" -- given
file_path = "file_upload/file_info/products/class1/file.csv" --given
incoming_file_path = url + "/" + quote(file_path)
df = pd.read_csv(incoming_file_path)
i am able to fetch or read my csv file successfully with the above code,But after my data get processed
i need to export that csv to some other folder, that i am not able to export. I did:
folder_to_export_path = "http://20.9.6.11:8066/file_info/products/processed_file"
clean_df.to_csv(r'folder_to_export_path+'filename.csv') # error
Try this:
folder_to_export_path = "http://20.9.6.11:8066/file_info/products/processed_file/"
clean_df.to_csv(folder_to_export_path+'filename.csv')
Reference: here
have you tried
clean_df.to_csv(folder_to_export_path+'/filename.csv')
or if using python 3.6+
clean_df.to_csv(f'{folder_to_export_path}/filename.csv')
Your last line has syntax error , try to correct it with the following :
clean_df.to_csv(folder_to_export_path+'/filename.csv')
First of all, with to_csv you need to pass a file path (or a file object).
So that makes your folder_to_export_path='http://20.9.6.11:8066/file_info/products/processed_file' invalid.
It should be
folder_to_export_path = 'file_info/products/processed_file/filename.csv' given that you want to export the CSV in file_info/products/processed_file directory.
Secondly, to_csv can create a file named filename.csv if it doesn't exist but it cannot create directories.
To create directories if they don't exist and then save a file you can do:
import os
import pandas as pd
folder_to_export_path = 'file_info/products/processed_file/'
if not os.path.exists(folder_to_export_path):
os.makedirs(folder_to_export_path)
pd.to_csv(os.path.join(folder_to_export_path, 'filename.csv'))
That should work.
How do you open an xls or csv file in python without having to connect the whole path?
ex: instead of using c:/user/...filename how do you connect it with just filename?
is it possible using pandas? This is in order to transfer the code from on console to another and the code being able to open with ease. From my understanding, if I use the path and send the code to another computer the excel page won't open there. btw the code will be sent with the original excel sheet
In this case, I believe you would have to set your working directory to the absolute path of your .py file.
Note, for the code below, your .csv file should be in the same directory as your .py file.
import os.path
import pandas as pd
base_dir = os.path.dirname(os.path.abspath(__file__)) # set directory to location of .py file
os.chdir(base_dir) # change directory
csv_file = pd.read_csv('file.csv',sep=',') # read .csv
Similar to solution of #Ira H., but instead of changing working directory you can generate full path:
import os.path
import pandas as pd
base_dir = os.path.dirname(
os.path.abspath(__file__)
) # set directory to location of .py file
csv_file = pd.read_csv(f"{base_dir}\\full_paths.csv", sep=",") # read .csv
This is a problem that has been previously solved (cannot write file with full path in Python) however I followed the advice in the previous answer and it didn't work and that's why I'm posting this.
I'm trying to access a csv file to load into the pandas dataframe.
import os
output_path = os.path.join('Desktop/My_project_folder', 'train.csv')
This is returning:
IOError: File Desktop/My_project_folder/train.csv does not exist
edit: I don't understand because the train.csv file exists in my project folder.
The os.path.join() function is platform agnostic meaning it can run across multiple OS (PC, Mac, Linux) without having the need to specify directories or subdirectories with forward or back slashes. Hence, simply separate paths and file names by commas:
myDir = '/path/to/Desktop/My_project_folder'
output_path = os.path.join(myDir, 'train.csv')
However, if Python script resides in same directory as data, have script detect its own path and then import data frame into pandas and avoiding hard-coding whole path names:
import os
import pandas as pd
# SET CURRENT DIRECTORY
cd = os.path.dirname(os.path.abspath(__file__))
traindf = read_csv(os.path.join(cd, 'train.csv'))