Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can use regular expressions (re) to search for the first date (out of two) in file names. Here's an example code snippet that uses re to search for the first date in file names:

import re
import os

directory = "/path/to/directory"
date_pattern = re.compile(r'\d{4}-\d{2}-\d{2}')

for filename in os.listdir(directory):
    match = date_pattern.search(filename)
    if match:
        print("First date in filename:", match.group())

In this example, we define a regular expression pattern (\d{4}-\d{2}-\d{2}) that matches a date in the format "YYYY-MM-DD". We use this pattern to search for dates in the file names in the specified directory (/path/to/directory).

If a match is found, we print the first date in the filename using the group() method of the Match object returned by search(). If multiple dates are found in a filename, this code will only print the first one.