Python Forum
Import Excel File that Starts with Number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Import Excel File that Starts with Number
#1
Hello all -

I have files I'm trying to import. I do not name these files, they're created by another department. The files start with a number, usually the year. Each time I try to do so I get an error message:

Error:
FileNotFoundError: [Errno 2] No such file or directory: 'N:\\Learing KPI Report\Background Files\X818 Online.xlsx'
The file name is '2018 Online' so not sure why it appears as 'X818' in the error message.

I'm using pandas to import:

import pandas as pd
df = pd.read_excel ("N:\Learing KPI Report\Background Files\X818 Online.xlsx")


I tried importing another file from the same folder that did not start with a number just for kicks and giggles and it imported just fine.

Any assistance is appreciated.

Thanks!
Reply
#2
if using python 3.6 or newer, you can use f-string to compile the file name:
>>> file_years = [2010, 2012, 2013, 2015, 2018]
>>> base_dir = 'N:/Learing KPI Report/Background Files/'
>>> for year in file_years:
...     filename = f'{base_dir}{year} Online.xlsx'
...     print(filename)
... 
N:/Learing KPI Report/Background Files/2010 Online.xlsx
N:/Learing KPI Report/Background Files/2012 Online.xlsx
N:/Learing KPI Report/Background Files/2013 Online.xlsx
N:/Learing KPI Report/Background Files/2015 Online.xlsx
N:/Learing KPI Report/Background Files/2018 Online.xlsx
>>> 
Note '/' instead of '\' avoids necessity of having to use escape sequence
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to count total number of sheets in an excel workbook using polars sayyedkamran 0 664 Oct-27-2023, 09:54 PM
Last Post: sayyedkamran
  Data Sorting and filtering(From an Excel File) PY_ALM 0 1,045 Jan-09-2023, 08:14 PM
Last Post: PY_ALM
  Colab, Github, starts yeeping50 2 3,082 Oct-23-2021, 05:33 PM
Last Post: itriIA
  How to import an xml file to Pandas sjhazard 0 2,358 Jun-08-2021, 08:19 PM
Last Post: sjhazard
  import columns of data from local csv file CatherineKan 2 3,337 May-10-2021, 05:10 AM
Last Post: ricslato
  Creating more than one excel File at once malvarez1976 0 1,815 Dec-15-2020, 02:04 AM
Last Post: malvarez1976
  How come afer some iterations of gradient descent, the error starts growing? Corpac 0 1,586 Mar-20-2020, 05:20 PM
Last Post: Corpac
  Convert Excel to .txt - Need each excel row to be separate .txt file cowboykevin05 2 4,793 Jan-03-2020, 06:29 PM
Last Post: Larz60+
  [split] Converting excel file to txt file unexceptionalhobby 2 4,363 Oct-16-2019, 06:34 PM
Last Post: unexceptionalhobby
  Python write result of VAR to excel file wissam1974 8 8,625 Jul-13-2019, 01:09 PM
Last Post: wissam1974

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020