Loading CSV into Pandas - no file directory - python

I need help debugging this code.
I am trying to add a csv file to my pandas data frame.
import pandas as pd
df = pd.read_csv ('batting.csv')
print(df)
When I execute this code I am getting this error:
FileNotFoundError: [Errno 2] No such file or directory: 'batting.csv'
I then tried to change the directory using os
os.getcwd()
os.chdir(r"C:\Users\crack\Downloads\excel\batting.csv")
I am now coming across this error:
NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\\Users\\crack\\Downloads\\excel\\batting.csv'
I am new to coding and have been looking for a solution to this error all day.

You could try ,
df = pd.read_csv(r"C:\Users\crack\Downloads\excel\batting.csv")
instead of
df = pd.read_csv ('batting.csv')

You are on the right track. The working directory is probably not where your file is located.
Try doing the following to see where it is:
print(os.getcwd())
The error you are seeing using os.chdir() is because you have specified a filename not a directory.
You have a few possible solutions:
Specify the full path to your CSV file:
pd.read_csv(r"C:\Users\crack\Downloads\excel\batting.csv")
Change the working directory to the same folder:
os.chdir(r"C:\Users\crack\Downloads\excel")
pd.read_csv("batting.csv")
If the script and CSV files are in the same folder and you don't want to specify a fixed path:
os.chdir(os.path.dirname(os.path.abspath(__file__)))
pd.read_csv("batting.csv")
This changes the working directory to be where the script is located. It takes the full script name and uses just the directory part.

Related

FileNotFoundError: [Errno 2] No such file or directory Pandas

I am trying to read my excel file with pd.read_excel in python.
But ı am get this error message = FileNotFoundError: [Errno 2] No such file or directory
I put my excel file same place with my python file.
pic1 pic2
You can use full path this way to read the excel file. And also add r prefix before the file path so backslash will be treated as literal character
pd.read_excel(r"C:\Users\selman\PycharmProjects\selman_learning\bisiklet_fiyatlari.xlsx")
import pandas as pd
path = 'absolute_path/records.xlsx' #eg. C:\\Projects\\readexcel\\file\\records.xlsx
df = pd.read_excel(path)
Do the above
I think you need to put file object and not only the path of the file.
Try to use:
with open("<path to your file>", encoding = 'utf-8') as f:
pandas.read_excel(f)
I think you can modify the code by writing it in this way:
pd.read_excel("./<file name>")
Providing the absolute path to the .xlsx file worked for me. For situations where you cannot anticipate what the absolute path will be, try the following:
import os.path
pd.read_excel(io=os.path.abspath('path\\to\\excel_file.xlsx'))
'path\to\excel_file.xlsx' should be the relative path to the .xlsx from the project root.
Credit to this answer to a similar question.

"[Errno 2] No such file or directory: 'Book10.xls'"

I am trying to combine several excel workbooks using jupyter notebook. I named the directory containing the excel workbooks files and its output is:
['Book10.xls',
'Book13.xls',
'Book12.xls',
'Book16.xls',
'Book9.xls',
'Book15.xls',
'Book14.xls',
'Book8.xls',
'Book18.xls',
'Book4.xls',
'Book6.xls',
'Book3.xls',
'Book1.xls']
I'm using the following loop to try to extract the data into python and paste them together in a larger dataframe:
df = pd.DataFrame()
for file in files:
if file.endswith('.xls'):
df = df.append(pd.read_excel(file), ignore_index=True)
df.head()
However, I'm the getting a path error:
[Errno 2] No such file or directory: 'Book10.xls'
I read a similar thread (Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory) which mentioned that using an IDE can change the file path. I also tried os.listdr() and os.getcwd to double check I'm in the correct directory (which I seem to be).
Any help on figuring out why the file path isn't being recognized and suggestions to get the loop working would be greatly appreciated!

Pandas, File not found error but file exists in directory [duplicate]

This question already has answers here:
open() gives FileNotFoundError / IOError: '[Errno 2] No such file or directory'
(8 answers)
Closed 7 months ago.
Opened VS code thru Anaconda3 and when trying to read a csv using pandas
df = pd.read_csv('file.csv')
My file.csv exists in same directory as my panda.py file but i receive a
FileNotFoundError: [Errno 2] File b'file.csv' does not exist: b'file.csv'
I can physically see the file in the same directory but my terminal says its not.
Why is this happening and how can i fix it?
${cwd} - the task runner's current working directory on startup.
The default setting of 'cwd' is the "${workspaceFolder}". In VSCode, the relative path depends on the setting parameter 'cwd', unless you use an absolute path. It doesn't care the relative path to your python file, it just cares the relative path to 'cwd'.
So you have two solutions to solve this problem:
First One:
Use the absolute path as you had tried and worked:
df = pd.read_csv(r'C:\Users\First Last\Documents\StatPython\file.csv')
or
df = pd.read_csv('C:\Users\irst Last\Documents\StatPython\file.csv')
Second One:
Take the path relative to default ${cwd}:
df = pd.read_csv('[the path of cwd][some paths]\file.csv')
In this case, seems like you haven't created a project of 'StatPython'. If it is your project name and opened by VSCode your code should be worked.
this is because you are using a DataFrame to read the csv file while the DataFrame module cannot do that.
Instead you can try using pandas to do the same operation by importing it using import pandas as pd and to read the file use pd.read_csv('filename.csv')
Still not sure why my code in question did not work, but this ended up working.
df = pd.read_csv(r'C:\Users\First Last\Documents\StatPython\file.csv')
Not only did i need the full path but an "r" before it.

python read_excel absolute path

I'm having to work with a file set in a directory structure I cant change
I'm having issues with trying to get the right path structure in excel.
Any thoughts?
df = pd.read_excel(r'/vagrant_data/xxx - Internal Documents/02 Work Packages/17 xxx/05 BoM/xxx')
IOError: [Errno 2] No such file or directory: '/vagrant_data/xxx - Internal Documents/02 Work Packages/17 xxx/05 BoM/xxx'
Try to go to that folder via terminal and execute pwd in the same directory where your excel file.

Relative path of excel files, python

I am trying to access an Excel file in Python, but I'm getting the following error:
FileNotFoundError: [Errno 2] No such file or directory: 'input/sales-feb-2014.xlsx'
Could you please help me defining the path of the file?
Python Code:
from xlrd import open_workbook
sheet = open_workbook('input/sales-feb-2014.xlsx').sheet_by_index(0)
print (sheet.cell_value(3,0))
You either need to add the full path to open_workbook(your_full_path_comes_here) or change the directory you are working in before.
You can use the osmodule to before calling your variable.
import os
os.chdir(r'...your/path/input')

Categories

Resources