I am trying to read an excel file into a dataframe using pandas and jupyter notebook from subfolder on my desktop. The file is on my desktop in a folder called 'Data', subfolder 'KN-Data', subfolder 'New-Files', file name "Customers.xlsx".
Here is the code I am trying:
df_customers = pd.read_excel (r"C:/Users/Zach/Desktop/Data/KN-Data/New-Files/Customers.xlsx")
Error is
[Errno 2] No such file or directory: 'C:/Users/Zach/Desktop/Data/KN-Data/New-Files/Customers.xlsx'
Try using double backslash instead of single forward slash. I always use double backslash and it works for me.
This is how ur code should look like:
df_customers = pd.read_excel (r"C:\\Users\\Zach\\Desktop\\Data\\KN-Data\\New-Files\\Customers.xlsx")
Related
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.
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 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!
I am coding a python program to read a data set file writing this line:
df = pd.read_csv (r'C:\Users\user118\Desktop\StudentsPerformance.csv')
This line works, but I have to upload this project as an assignment , so the computer path must be changed. I think about putting the csv file in the project folder and i did and wrote this line:
df = pd.read_csv ("StudentsPerformance.csv")
but it gave me an error saying that the file isn't found. Where to correctly put the file in the project folder? Or what I should do?
To read your csv file with a df = pd.read_csv ("StudentsPerformance.csv") line you should put it right next to executing .py file.
To do that you can just read your csv file using full path like this:
df = pd.read_csv (r'C:\Users\user118\Desktop\StudentsPerformance.csv')
and than write df.to_csv('StudentsPerformance.csv')
After that you would be able to read your csv as you wanted using
df = pd.read_csv('StudentsPerformance.csv')
I have some excel files in a folder. I use the below code to read those excel files and get them into a list so that I can pass that list into a loop to get particular data from all those files.
My problem is - If I open a excel file from that folder and run the script.The opened excel file instance is created in the folder and the script now takes that temporary instance as an .xlsx file and returms it in the list and passes it to the loop where it eventually fails as -"No such directory" I found a way of avoiding the failure by adding in a "-1" from lenght of list to loop.But this isnt effective.
Please suggest any alternatives for os.chdir
import pandas as pd
import glob
import os
os.chdir(r'\\servername/Files_to_Read')
files = [i for i in glob.glob('*.{}'.format('xlsx'))]
print(files)
s = 0
while(len(files) > s):
print(files[s])
df_getvalues = pd.read_excel(files[s], sheet_name="LISTS", header=None)
dfindx = (df_getvalues.index)
print("This is the index of the file - " +str(dfindx))
print(df_getvalues.iloc[dfindx,0])
s = s + 1
error:-
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\$name_of_file.xlsx'
The error states that it is searching for the file in C drive but actual folder of excel files is on H drive.
Note - Im using Windows 10, Excel 2016 , python 3.7
This issue seems to be very intermittent and this never occurred again after I did a reboot and I also had some issues with my VM migration and my profile has been configured manually so I dont know what among the various reasons helped me to get out of this issue.
Also, I am not using OS.Chdir now . I am using path and list directory this time as I saw somewhere that chdir is not so recommended.
list_dirctry_content = os.listdir(src)
for xlfile in list_dirctry_content:
str_name_file = os.path.join(src, xlfile)
if(str_name_file.endswith('.xlsx')):
final_driving_list.append(str_name_file.strip(src))