Answers for "get latest file in folder python"

1

python get newest file in directory

import glob
import os

list_of_files = glob.glob('/path/to/folder/*') # * means all if need specific format then *.csv
latest_file = max(list_of_files, key=os.path.getctime)
print(latest_file)
Posted by: Guest on February-03-2021
1

python get latest edited file from any directory

from os.path import expanduser
import os
home = expanduser("~")
latest_edited_folder = max([f for f in os.scandir(home)], key=lambda x: x.stat().st_mtime).name
search_on = home+'\'+latest_edited_folder
latest_edited_file = max([f for f in os.scandir(search_on)], key=lambda x: x.stat().st_mtime).name
Posted by: Guest on September-12-2021

Code answers related to "get latest file in folder python"

Python Answers by Framework

Browse Popular Code Answers by Language