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

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!

Related

Loading CSV into Pandas - no file directory

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.

Reading XLSX data from subfolders on desktop

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")

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- os.chdir takes an open excel file instance into account while execution

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))

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.

Categories

Resources