0

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)
BigBen
  • 46,229
  • 7
  • 24
  • 40
Martina
  • 43
  • 7
  • is the date in the filename? – White Wizard Jan 03 '23 at 13:36
  • 2
    `Popen` is used to run a shell command. You probably meant to use `open`. More to the point, though: you could get the list of CSVs in the download folder, sort them by creation date, and open the newest one. – Julia Jan 03 '23 at 13:37
  • 2
    Or you could generate the file name and open it directly (assuming it’s predictable). – Julia Jan 03 '23 at 13:38

1 Answers1

1

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)
Surjit Samra
  • 4,614
  • 1
  • 26
  • 36