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.
Related
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.
can someone tell me why when I want to open a text file with python
my output is FileNotFoundError: [Errno 2] No such file or directory: 'demofile.txt'
even though the file is already in the same folder and the writing is correct.
and this is my code
f = open("demofile.txt", "r")
print(f.read())
thank you before
The path of the Python file and the current working directory can differ. open uses the current working directory if you use a relative path. The obvious fix is to use an absolute path. But then you will have to edit the code each time you copy the script to a different folder.
You can use pathlib to create an absolute path based on the current running scripts location. Put this in a Python script, run it and look at the result.
import pathlib
print(pathlib.Path(__file__).parent)
print(pathlib.Path(__file__).parent / 'demofile.txt')
print(pathlib.Path(__file__).parent / 'data' / 'demofile.txt')
So your code can be changed to
filepath = pathlib.Path(__file__).parent / 'demofile.txt'
with open(filepath, 'r') as f:
print(f.read())
Try using with
with open("demofile.txt","r") as f:
f.read()
Maybe, You are executing python file with absolute path that's why FileNotFound.
Try with absolute path. Like, c:\files\demofile.txt
I solved the problem with the answer of Mathias, I had the same problem but with an image, then you need to write
import pathlib
print(pathlib.Path(__file__).parent)
print(pathlib.Path(__file__).parent / 'name_of_your_file.extension')
print(pathlib.Path(__file__).parent / 'data' / 'name_of_your_file.extension')
already done the correct answer is to use the name of the specific folder where the text files are located
thank you very much, everyone
i have the following code.and i have the csv file located in the path but when i run the code it just says that the file dosent exist at all.i moved the code to diff locations and changed the path to the new one and i also changed the name of the file but it didnt worked.also i add ".csv" to the name of the file but no good came of it.
here is the code:
import pandas as pd
import numpy as np
import csv
filename = ("C:\\Users\\Z\\pima-indians-diabetes.data.csv")
df1 = pd.read_csv(filename)
print(df1.head())'''
The error says
FileNotFoundError: [Errno 2] File C:\Users\Z\pima-indians-diabetes.data.csv does not exist: 'C:\\Users\\Z\\diabetes'
You need to give the *.csv file extension in your code. Replace
filename = ("C:\\Users\\Z\\diabetes")
with
filename = "C:\\Users\\Z\\diabetes.csv"
Few steps to check whether the file indeed exists:
Open a Command Prompt (Click Start and type cmd)
Type this command dir C:\Users\Z\pima-indians-diabetes.*
This will clearly show if the file in question really exists.
It might not be there at all.
Or it might be named C:\Users\Z\diabetes.csv.csv.
Latter can happen when saving a file with Save As and typing my-file.csv for the file name, unaware that the application being used also adds the extension .csv, so now there are two of those.
Here is the expected command output for an existing file with correct name:
C:\Users\Z>dir C:\Users\Z\pima-indians-diabetes.*
983 pima-indians-diabetes.data.csv
1 File(s) 983 bytes
0 Dir(s) 220,040,663,040 bytes free
remove '.' from your filename. I think it is effecting your file format
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')
I have simple code:
from xlrd import open_workbook
open_workbook('Book1.xls')
No matter how I save the file (xls or xlsx) or how I change the path to the file (C:\Users\... or C:\Users...) I keep getting:
IOError: [Errno 2] No such file or directory: '(insert whatever is in place of Book1.xls)'
I don't know what I could be doing wrong. I'm not trying anything specific yet I just want it to not throw up an error.
You are not including whole path of the file. The path will be looks like:
file="C:\\USER\\TEST\\FILENAME"
or
file=r":\USER\TEST\FILENAME"
If you are using single slash then you need to use 'r' or else it will throw error.
you are not doing workbook("Book1.xls")
you are passing it a path (with directories) ... one of the folders in the path doesnt exist
this is probably because you do something like
directory="C:\Users\Blah\..."
when you need to do
directory = r"C:\Users\Blah\..."
other wise the backslashes serve to escape the next character