I am trying to read excel file in pycharm using pandas. I installed the package successfully. My issue is that I am trying to use file location in addition to its name I tried many thing as follow:
JavaScript
x
5
1
import pandas as pd
2
fileLocation = "C:\Users\GTS\Desktop\Network Interdiction Problem\Manuscript\Interdiction_Data.xlsx"
3
fileName = 'Interdiction_Data.xlsx'
4
data = pd.read_excel('fileLocation'+'fileName')
5
However I keep receiving the following error
JavaScript
1
17
17
1
Traceback (most recent call last):
2
File "C:/Users/GTS/PycharmProjects/Reliability1/Reliability1.py", line 4, in <module>
3
data = pd.read_excel('fileLocation'+'fileName')
4
File "C:UsersGTSPycharmProjectsReliability1venvlibsite-packagespandasioexcel_base.py", line 304, in read_excel
5
io = ExcelFile(io, engine=engine)
6
File "C:UsersGTSPycharmProjectsReliability1venvlibsite-packagespandasioexcel_base.py", line 824, in __init__
7
self._reader = self._engines[engine](self._io)
8
File "C:UsersGTSPycharmProjectsReliability1venvlibsite-packagespandasioexcel_xlrd.py", line 21, in __init__
9
super().__init__(filepath_or_buffer)
10
File "C:UsersGTSPycharmProjectsReliability1venvlibsite-packagespandasioexcel_base.py", line 353, in __init__
11
self.book = self.load_workbook(filepath_or_buffer)
12
File "C:UsersGTSPycharmProjectsReliability1venvlibsite-packagespandasioexcel_xlrd.py", line 36, in load_workbook
13
return open_workbook(filepath_or_buffer)
14
File "C:UsersGTSPycharmProjectsReliability1venvlibsite-packagesxlrd__init__.py", line 111, in open_workbook
15
with open(filename, "rb") as f:
16
FileNotFoundError: [Errno 2] No such file or directory: 'fileLocationfileName'
17
Any Idea?
Thanks in advance
Advertisement
Answer
Your fileLocation
variable includes the name of the file. reading fileLocation + fileName
is essentially reading
C:\Users\GTS\Desktop\Network Interdiction Problem\Manuscript\Interdiction_Data.xlsxInterdiction_Data.xlsx
Another issue is that you have quotation marks around your variable names when calling pd.read_excel()
meaning that you are passing a string to the function.
Try:
JavaScript
1
2
1
data = pd.read_excel(fileLocation)
2