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')
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.
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.
This question already has answers here:
How to reliably open a file in the same directory as the currently running script
(9 answers)
Closed last year.
I am stuck to this weirdly small problem for like 3 hours and i just can't find the fix of it. Here look at the problem I'm trying to read a .txt file(greetings.txt) which contains a simple message and both- the .txt file and the program to read the file (readingfile.py) are inside the same folder, but still when i try to run the program which should return the text inside the .txt file , instead , it's throwing an error FileNotFoundError: [Errno 2] No such file or directory: 'greetings.txt' If you think you familiar with this issue please help.
The code is written below and i will be also attaching an image please check that as well.Go through this link to view the image
with open('greetings.txt') as file:
content = file.read()
print(content)
Your corrent folder is not the parent folder of the script, which is coincidentally also where your greetings.txt file is located.
To fix that, use __file__ to get the absolute path of the executing script, and then join with the parent path to get the absolute path of the text file.
For instance:
import os
path_to_file = os.path.join(os.path.dirname(__file__), 'greetings.txt')
with open(path_to_file) as file:
...
Alternatively, in Python 3.x, the above code can be simplified even further by relying on pathlib module:
from pathlib import Path
content = (Path(__file__).parent / 'greetings.txt').read_text()
I am trying to read in a netcdf file with Spyder(python 3.7) from the Anacondas package and I am getting the error message:
[Errno -51] NetCDF: Unknown file format: b'AMSR2-MBT_v2r0_GW1_s201907081630360_e201907081809340_c201907081825230.nc'
I've tried setting the paths using import os and such other methods. It seems like it may be trying to use the path as the filename or there may be an issue with the netCDF installation
I also tried joining AMSR2_path to the filename as the MBT_data variable but that didn't help.
import os
import netCDF4
#os.getcwd() #Use to check the current location
#os.chdir() #Use to change directory
AMSR2_path = os.path.join(os.getcwd()) #Returns the current working
directory
MBT_data = "AMSR2-
MBT_v2r0_GW1_s201907081630360_e201907081809340_c201907081825230.nc"
print(type(MBT_data))
data = netCDF4.Dataset(MBT_data,"r")
print(data.variables)
Here is the actual file.
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.