0

When we deal with repetitive tasks, e.g. you have a weekly process that get an excel file in the same format, but different numbers. How do we ask Python to only read the most recently added or modified file in the folder (assuming the folder is used to keep all the historical files)?

Of course we can assign a excel file name and use pandas or other libraries to read it. But since I do NOT need to import the previous file, and I do NOT want to open the .py file to update the excel file name, I hope to find a way to automate the process.

DanZimmerman
  • 1,626
  • 6
  • 23
  • 45
  • What have you tried so far? The `os` module, for example, provides a variety of tools for interrogating folders and files. – asongtoruin Apr 06 '17 at 16:12
  • 2
    [How to get file creation & modification date/times in Python?](http://stackoverflow.com/q/237079/953482) may be of use to you. – Kevin Apr 06 '17 at 16:12
  • Possible duplicate of [How to get file creation & modification date/times in Python?](http://stackoverflow.com/questions/237079/how-to-get-file-creation-modification-date-times-in-python) – waterproof Apr 06 '17 at 16:27

2 Answers2

0

Read the modification time of a file using os.path.getmtime.

import os                                                                   
import glob             

excel_folder = 'C:/Users/ThisOne/ExcelStuff/'

# glob.glob returns all paths matching the pattern.
excel_files = list(glob.glob(os.path.join(excel_folder, '*.xls*')))

mod_dates = [os.path.getmtime(f) for f in excel_files]

# sort by mod_dates.
file_date = zip(excel_files, mod_dates).sort(key=lambda d: d[1])

newest_file_path = file_date[0][1]

A good response to a similar question here.

Community
  • 1
  • 1
0
import os
import glob

os.chdir(r"H:\file_input_data_excel")
def read_folder():
    while True:
        print("------Đang quét file-----")
        for file in glob.glob("*.xls*"):
            print(file)
    time.sleep(10)
David Buck
  • 3,752
  • 35
  • 31
  • 35