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
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))
today I struggle to make Jupyter read a csv file I have downloaded. When it was saved like this "C:\Users\argir\state.csv" Jupyter was able to read it. (photo: https://ibb.co/qYLmWb0 ). But when I put it in a folder and the path was "C:\Users\argir\datasets_dika_mou\state.csv" (photo: https://ibb.co/mBVQS3f ) or when it was saved in another folder on the desktop and the path was C:\Users\argir\Υπολογιστής\datasets from 50 practical-statistics-for-data-scientistsebook oreilly\state.csv" (photo: https://ibb.co/M7m3PTF ) the Jupyter couldn't read it
FileNotFoundError: [Errno 2] No such file or directory: 'state.csv'
Why does this happen?
import pandas as pd
state_data = pd.read_csv("state.csv")
From the code you put into the comment, it could be that you simply not defining the path to the file.
When your file is at the same directory as the Notebook your code will work, but when the file is not at the same path you have to tell python where to look.
like:
import pandas as pd
state_data = pd.read_csv("datasets_dika_mou/state.csv")
I have few csv files in a folder which is present in the same folder where my program is stored. I am using following code to read the csv files:
import pandas as pd
import os
files = os.listdir('../review_data')
for file in files:
os.chdir('../review_data')
temp = pd.read_csv('../review_data/'+file,header=None)
print(temp)
However, when I ran the above code, the print statement prints nothing. When I ran the same code (with some modification in file path ) in windows it works fine. However, it did not work in MAC. Could anyone help me where I am making the mistake?
import pandas as pd
hand_1=pd.DataFrame({
'Tables of 5':[5,10,15,20,25],
'Tables of 6':[6,12,18,24,30]})
hand_1.to_csv('Tables.csv')`
How do i find out where Tables.csv is stored?
Is this where python stores csv files by default and can this be changed?
It will be saved in your current working directory. If you would like to learn it, you can use the following code:
import os
current_directory = os.getcwd()
You can give a full path instead of tables.csv to store in another directory.
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'))