1

I did a Python application which needs to open an excel file to find data inside.

Every time this excel file has a new version its name is also changed like test_V1.2.xlsx to test_V1.3.xlsx so my path becomes incorrect.

I have no idea how to correct this dynamically, is it possible to point on test_V like with a regular expression?

my path looks like this :

wb = xl.Workbooks.Open(r'C:\Users\300231823\Desktop\GUI\simplenew4_V1.1.xls')

When someone change the file version to simplenew4_V1.2.xls my variable WB do not point on my excel file and my program do not works

  • 1
    We will need more code to help you here. But perhaps you can split the filename by `_` and remove the extension. Then you'll have the version (like `"V1.2"`). – jkr Jul 19 '22 at 18:42

1 Answers1

0

If you know the prefix is always the same you can use os.listdir() to get what is in the folder.

here's code:

import os

prefix = "test_V"
folder_path = r"C:\Users\300231823\Desktop\GUI"

for file in os.listdir(folder_path):
    if prefix in file:
        excel_file = os.path.join(folder_path, file)

wb = xl.Workbooks.Open(excel_file)
Arthur
  • 99
  • 7
  • It'll work if there's only one version at the time you run it. – Arthur Jul 19 '22 at 19:02
  • 1
    Yes I confirm, this is exactly what I was looking for. You rocks ! – Zap2Cynops Jul 19 '22 at 19:06
  • Hello Arthur, I have permission issue using this method, here the code I wrote (after your suggestion :root=Tk() root.title('24x99') root.geometry("630x300") wb=Workbook() prefix = "Spec varmus Malus" filepath=r"\\nas2-pp.ppp.com\users\_BPM\Aba\5 - Malus\Proc" for file in os.listdir(filepath): if prefix in file: excel_file = os.path.join(filepath, file) wb=load_workbook(excel_file) sheet = wb['Spec'] – Zap2Cynops Jul 19 '22 at 20:31