Issue with file versioning with python

Viewed 37

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 Answers

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)
Related