1

I am looking for a way in which I can get the latest modified file in a folder using Python.

I have tried looking online for some solution but have had no luck so far with the OS module in Python. Maybe there is another module I should be using? This is my code so far:

os.listdir('G:\\Data\\Hotels\\FTP\\Hotel Data\\IDEAS')

From this list I want the last modified file. I am new to Python, so not sure what modules are available to me in order to look at the time modified for each file.

Sorath
  • 543
  • 3
  • 10
  • 32
  • I am new to Python so none of that makes any sense to me! @DavidG I was hoping for more help on building on what I already have – Sorath May 13 '19 at 08:53
  • simple answer: https://codereview.stackexchange.com/a/120500 – Makhele Sabata May 13 '19 at 08:55
  • Use `os.path.getmtime(path)` on each filename to get the last modified time. – cdarke May 13 '19 at 08:57
  • @Sorath Your question's closed unfairly; here's one approach: `import os; from pathlib import Path; paths = [str(p) for p in Path(your_dir).iterdir() if p.is_file()]; paths.sort(key=os.path.getmtime); most_recent_path = paths[-1]` – OverLordGoldDragon Mar 19 '20 at 13:42

0 Answers0