python read_excel absolute path - python

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.

Related

How to open file in python?

Iam coding in vs code with python3.
in the tutorial says that you should write
f=open("filename.txt")
and then run it and if you want to read it
f=open("filename.txt","r")
and I write a txt in my computre and I'm sure its exicts in my loptop but every time I run the code it gives me :(FileNotFoundError: [Errno 2] No such file or directory)
what should I do?
You can try to change "filename.txt" for the absolute path of that file. That means you should go to your file and copy the exact complete path from the storage through all directories until reaching it:
f=open("filename.txt","r")
f=open(r"C:\\Users\\your_user\\Desktop\\filename.txt","r") #Example of an absolute path, in this case the file is in Desktop but you have to copy the exact path you have
If that doesn't work, the file might be corrupted or defective, so you would have to try other solutions.

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.

"[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!

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 os.rename can't find a file, even though the code check it exists

I have a strange error:
in the code:
if os.path.isfile(df_file):
os.rename(df_file, df_file + '_backup.parquet')
I am getting an error:
os.rename(df_file, df_file + '_backup.parquet')
FileNotFoundError: [Errno 2] No such file or directory: '/data/5faf97ca101ae0413c383678186bf601.parquet' ->
'/data/5faf97ca101ae0413c383678186bf601.parquet_backup.parquet'
Even though I checked and the file exist!
what could be the problem ?
The source file and directory are obviously exist
The code is called within a flask thread.
'/data/5faf97ca101ae0413c383678186bf601.parquet_backup.parquet' This is an absolute path, which points to the data directory under the root path. I guess you want to use a relative path,'./data/5faf97ca101ae0413c383678186bf601.parquet_backup.parquet'

Categories

Resources