简体   繁体   中英

No such file or directory error when running program in anaconda prompt

I have been making a simple python script to practice reading in and writing to an excel file. I have a couple simple functions that create a dataframe from the excel file, allows the user to create a new row (ie updated savings balance etc.), then append the dataframe with the new row on the bottom and save it back to excel.

Basically every function begins with: dfTest = pd.read_excel('FinancesExcel.xlsx', sheet_name='Test')

and writes to with:

    dfTestNew = dfTest.append(new_row,ignore_index=True)
    
    with pd.ExcelWriter('FinancesExcel.xlsx') as writer:
        dfTestNew.to_excel(writer, sheet_name='Test',index=False) 

When I run it in Spyder it works great. Updates and saves no problem, as many times as you want before exiting.

When I try to run it in Anaconda Prompt it loads the main menu...

    i = 0
    while i != 1:
    print("Main Menu")
    
    selection = input('1 - Quick Update\n'
                      '2 - Full Update\n'
                      '3 - Overview\n'
                      '4 - Visualize\n'
                      '5 - Forecast\n'
                      'Selection: ')

Currently only options 1-3 are built out but they all begin with: dfTest = pd.read_excel('FinancesExcel.xlsx', sheet_name='Test')

If you select any of the options 1-3 it yeilds...

Traceback (most recent call last):

  File "D:\Python\Projects\PersonalFinance\PersonalFinanceTest.py", line 273, in <module>
    dfTest = pd.read_excel('FinancesExcel.xlsx', sheet_name='Test')
  File "D:\michaData\lib\site-packages\pandas\io\excel\_base.py", line 304, in read_excel
    io = ExcelFile(io, engine=engine)
  File "D:\michaData\lib\site-packages\pandas\io\excel\_base.py", line 824, in __init__
    self._reader = self._engines[engine](self._io)
  File "D:\michaData\lib\site-packages\pandas\io\excel\_xlrd.py", line 21, in __init__
    super().__init__(filepath_or_buffer)
  File "D:\michaData\lib\site-packages\pandas\io\excel\_base.py", line 353, in __init__
    self.book = self.load_workbook(filepath_or_buffer)
  File "D:\michaData\lib\site-packages\pandas\io\excel\_xlrd.py", line 36, in load_workbook
    return open_workbook(filepath_or_buffer)
  File "D:\michaData\lib\site-packages\xlrd\__init__.py", line 111, in open_workbook
    with open(filename, "rb") as f:

FileNotFoundError: [Errno 2] No such file or directory: 'FinancesExcel.xlsx'

FinancesExcel.xlsx is definitely there, where it always is when it runs in Spyder. I don't have the file open when I run the script (already learned that lesson).

I am unable to figure out why it will not run.

check your current working directory when you are running the script in Anaconda Prompt:

import os

print(f'your current wd is: {os.getcwd()}')

I am confident that it is different than the one you are running from the Spyder. You need to provide the full path to your Excel file. "path/to/excel/FinancesExcel.xlsx"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM