How to get the Latest File in a Folder using Python

In this guide, you’ll see how to get the latest file in a folder using Python.

To start, here is a template that you may use to get the latest file:

import glob
import os.path

folder_path = r'path where your files are located'
file_type = '\*type'
files = glob.glob(folder_path + file_type)
max_file = max(files, key=os.path.getctime)

print (max_file)

In the next section, you’ll see a simple example with the steps to get the latest file for a given folder. Also included is a simple GUI to import your latest file:

How to get the Latest File in a Folder using Python

Steps to get the Latest File in a Folder using Python

Step 1: Capture the Path where the files are stored

To begin, capture the path where your files are currently stored.

For example, the following files are stored in a ‘Test’ folder:

C:\Users\Ron\Desktop\Test

Example of path

You’ll notice that there are two CSV files in the folder, where the:

  • First file, called ‘Cars,’ was created on 2020-01-09 8:04 PM
  • Second file, called ‘Products,’ was created 6 minutes later on 2020-01-09 8:10 PM

The ultimate goal is to get the latest file created. In this case, it would be the ‘Products’ file.

How to get the Latest File in a Folder using Python

Step 2: Get the Latest File in the Folder using Python

You may now use this template to get the latest file in a folder using Python:

import glob
import os.path

folder_path = r'path where your files are located'
file_type = '\*type'
files = glob.glob(folder_path + file_type)
max_file = max(files, key=os.path.getctime)

print (max_file)

In the context of our example:

  • The path where the files are located is: C:\Users\Ron\Desktop\Test
  • The file type is: CSV

So the complete Python code would look as follows (don’t forget to place the ‘r‘ character before your path to avoid any errors in the path name):

import glob
import os.path

folder_path = r'C:\Users\Ron\Desktop\Test'
file_type = '\*csv'
files = glob.glob(folder_path + file_type)
max_file = max(files, key=os.path.getctime)

print (max_file)

And this is the result:

Path example of csv file

What if you want to import the latest file into Python?

If that’s the case, proceed to step 3 below.

Step 3 (optional step): Import the Latest File into Python

You can use Pandas in order to import a CSV file into Python.

If you haven’t already done so, install the Pandas package. You may use this command to install Pandas under Windows:

pip install pandas

Finally, apply the code below in order to import the latest csv file:

import glob
import os.path
import pandas as pd

folder_path = r'C:\Users\Ron\Desktop\Test'
file_type = '\*csv'
files = glob.glob(folder_path + file_type)
max_file = max(files, key=os.path.getctime)

import_file = pd.read_csv(max_file)
print (import_file)

You’ll then see the following data from the ‘Products’ file:

DataFrame Example

Tool to Import the Latest File

Lastly, here is the syntax to import the latest file using the tkinter package:

import glob
import os.path
import pandas as pd
import tkinter as tk

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 500, height = 250)
canvas1.pack()

label0 = tk.Label(root, text='Import Latest File')
label0.config(font=('helvetica', 15))
canvas1.create_window(250, 45, window=label0)

entry1 = tk.Entry (root, width = 40) 
canvas1.create_window(350, 100, window=entry1)

entry2 = tk.Entry (root, width = 40) 
canvas1.create_window(350, 140, window=entry2)

label1 = tk.Label(root, text='Enter Directory Path:')
label1.config(font=('helvetica', 10))
canvas1.create_window(150, 100, window=label1)

label2 = tk.Label(root, text='Enter File Type:')
label2.config(font=('helvetica', 10))
canvas1.create_window(165, 140, window=label2)

def importFile ():  
    folder_path = entry1.get()
    file_type = entry2.get()
    file_type = '\*' + file_type

    files = glob.glob(folder_path + file_type)
    max_file = max(files, key=os.path.getctime)
    
    import_file = pd.read_csv(max_file)
    print (import_file)
  
button1 = tk.Button(text='Import File', command=importFile, bg='brown', fg='white', font=('helvetica', 10, 'bold'), width = 20)
canvas1.create_window(250, 200, window=button1)

root.mainloop()

Run the code in Python, and you’ll see the following Graphical User Interface (GUI):

How to get the Latest File in a Directory using Python

Type the path of the folder/directory, and the file type:

Import the Latest File in a Directory using Python

Click on the ‘Import File’ button and you’ll get:

Import data