To get the latest file in a folder using Python (where “type” represents the file type, such as csv, or txt):
import glob
import os.path
folder_path = r"path where your files are located"
file_type = r"\*type"
files = glob.glob(folder_path + file_type)
max_file = max(files, key=os.path.getctime)
print(max_file)
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, assume that 2 CSV files are stored in a ‘Test‘ folder, where the path to the folder is:
C:\Users\Ron\Desktop\Test
The names, and the creation date, of the files, are as follows:
Products 2024-01-09 8:10 PM
You’ll notice that the:
- First file, called Furniture, was created on 2024-01-09 8:04 PM
- Second file, called Products, was created 6 minutes later on 2024-01-09 8:10 PM
The ultimate goal is to get the latest file created. In this case, it would be the ‘Products’ file.
Step 2: Get the Latest File in the Folder using Python
To get the latest CSV file in the folder using Python:
import glob
import os.path
folder_path = r"C:\Users\Ron\Desktop\Test"
file_type = r"\*csv"
files = glob.glob(folder_path + file_type)
max_file = max(files, key=os.path.getctime)
print(max_file)
Run the code in Python (adjusted to you path), and you’ll get the latest CSV file (here it is ‘Products’):
C:\Users\Ron\Desktop\Test\Products.csv
Step 3 (optional step): Import the Latest File into Python
You can use Pandas in order to import the latest CSV file into Python:
import glob
import os.path
import pandas as pd
folder_path = r"C:\Users\Ron\Desktop\Test"
file_type = r"\*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:
product_id product_name price
0 1 Oven 800
1 2 Microwave 350
2 3 Toaster 80
3 4 Refrigerator 1100
4 5 Dishwasher 900