Home > Software engineering >  Open last saved CSV file via python
Open last saved CSV file via python

Time:01-04

I have a file to be downloaded in CSV format every day that it's name changes daily. how can i open that file via python in Excel after being downloaded automatically?

I've tried the below solution but i have to mention the file name in full which I can't do because it changes dynamically.

from subprocess import Popen
p = Popen('filename.csv', shell=True)

CodePudding user response:

At the end of your code that downloads the file You can find latest file in your download folder as and open it

import glob
import os
from subprocess import Popen

list_of_files = glob.glob('/path_to_download_folder/*.csv') 
latest_csv_file = max(list_of_files, key=os.path.getctime)
print(latest_csv_file)
#then do what ever you want to do with it.
os.startfile(latest_csv_file)
  • Related