DEV Community

Vidyasagar SC Machupalli
Vidyasagar SC Machupalli

Posted on

Finding Today's Changed Files: A Quick Python Script for File Uploads

My friend recently asked me for help with a common problem: they needed to upload files to a remote server, but didn't want to upload everything—just the files that had changed today. Manually checking modification dates for dozens of files? Not fun.

So I wrote a quick Python script to solve it.

The Problem

When you're syncing files to a remote server, uploading everything is inefficient and time-consuming. You really only need the files you've modified today—whether that's code you've been working on, documents you've edited, or data files you've processed.

The Solution

This get_latest_files_by_today() function scans a folder and returns only the files modified today, sorted with the most recent first:

def get_latest_files_by_today(folder_path: str) -> list:
Enter fullscreen mode Exit fullscreen mode

It's straightforward:

  1. Validates the folder path to avoid errors
  2. Compares each file's modification date to today's date
  3. Sorts results by time (newest first)
  4. Returns clean filenames ready to use

The key insight is comparing just the date portion—ignoring the specific time—so anything touched today qualifies:

today = datetime.now().date()
modified_date = datetime.fromtimestamp(modified_timestamp).date()
if modified_date == today:
    today_files.append((file_path, modified_timestamp))
Enter fullscreen mode Exit fullscreen mode

Real-World Usage

My friend now runs this script at the end of their workday:

latest_files = get_latest_files_by_today("/path/to/project")
print(latest_files)
Enter fullscreen mode Exit fullscreen mode

The output gives them an instant list of exactly which files need uploading to the remote server. No guesswork, no manual checking, no accidentally missing a file.

Beyond File Uploads

While we built this for upload workflows, it's useful anytime you need to track today's file activity:

  • Creating daily backups of changed files
  • Reviewing what you worked on before leaving for the day
  • Monitoring build outputs or logs
  • Checking which documents were edited during a meeting

Simple Tools, Big Impact

This script uses only Python's standard library—no external dependencies, no complex setup. Just point it at a folder and get your results.

Sometimes the most helpful code isn't the most sophisticated. It's the utility that solves a real problem in 40 lines, saves you 15 minutes every day, and just works.

Got a folder that needs smart file filtering? Give this script a try.

import os
from datetime import datetime

def get_latest_files_by_today(folder_path: str) -> list:
    """
    Retrieves and sorts files in a given folder, returning only those modified today.

    Args:
        folder_path (str): The path to the folder to scan.

    Returns:
        list: A list of full filenames (name + extension) of files modified today,
              sorted by modification time (latest first).
    """
    if not os.path.isdir(folder_path):
        print(f"Error: Folder '{folder_path}' does not exist.")
        return []

    today = datetime.now().date()
    today_files = []

    for filename in os.listdir(folder_path):
        file_path = os.path.join(folder_path, filename)

        if os.path.isfile(file_path):
            modified_timestamp = os.path.getmtime(file_path)
            modified_date = datetime.fromtimestamp(modified_timestamp).date()

            if modified_date == today:
                today_files.append((file_path, modified_timestamp))

    # Sort files by modification time in descending order (latest first)
    today_files.sort(key=lambda x: x[1], reverse=True)

    # Extract only the full filenames
    sorted_filenames = [os.path.basename(file_path) for file_path, _ in today_files]

    return sorted_filenames

# Example usage:
current_directory = os.getcwd()
latest_files = get_latest_files_by_today(current_directory)
print(latest_files)

# Or with a specific folder:
# my_folder = "/path/to/your/folder"
# latest_files_in_folder = get_latest_files_by_today(my_folder)
# print(latest_files_in_folder)
Enter fullscreen mode Exit fullscreen mode

output:

/Users/vmac/Documents/Code/Python/.venv/bin/python /Users/vmac/Documents/Code/Python/latestfile.py 
Files changed today ['latestfile.py', 'abc.py']

Process finished with exit code 0
Enter fullscreen mode Exit fullscreen mode

Top comments (0)