How can I search for specific part of strings in a list and list them? Python 2.7
<p>With some help I'm hoping to build a list of strings from a bunch of text files whereby the date stamp of the filename is older than three days and the output list only contains part of the strings i.e. filename = 2016_08_18_23_10_00 - playlist, string in file is E:\media\filename.mxf or D:\media2\filename.mxf. I wish for the list to contain only the filename.mxf for example. So far I have the following:</p>

<pre><code>## imports modules ##

from datetime import datetime, timedelta
import os
import re

## directory ##

path = r"C:\Users\michael.lawton\Desktop\Housekeeper\Test"

## days to subtract variable ##

days_to_subtract = 3

## re-import datetime module ##

import datetime

## finds all files with date and time stamp. If statement is true adds them to list ##

lines = []
for filename in os.listdir(path):
  date_filename = datetime.datetime.strptime(filename.split(" ")[0], '%Y_%m_%d_%H_%M_%S')
  if date_filename &lt; datetime.datetime.now()- datetime.timedelta(days=days_to_subtract):
    with open(os.path.join(path, filename), 'r') as f:
      lines.extend(f.readlines()) # put all lines into array

## opens files in array ##

  print filename # debug
  file = open(os.path.join(path,filename), 'r')
  print file.read() # debug
  rasp = file.read()

## search for all strings containing .mxf from array ##

import fnmatch
import os.path
pattern = "*.mxf"
matching = [os.path.basename(s) for s in file if fnmatch.fnmatch(s,  pattern)]
print matching

# currently the output is empty i.e. []
</code></pre>

<p>If I understood you correctly, you want to put all the files of type "mxf" that are older than three days into an array. You are doing things more complicated then you have to. Simply check if the filename matches the pattern you have defined and add those files to an array (e.g matchingfiles):</p>

<pre><code>## imports modules ##

from datetime import datetime, timedelta
import os
import re
import fnmatch
import os.path

## directory ##

path = r"C:\Users\michael.lawton\Desktop\Housekeeper\Test"

## days to subtract variable ##

days_to_subtract = 3

pattern = "*.mxf"

## finds all files with date and time stamp. If statement is true adds them to list ##

matchingfiles = []
for filename in os.listdir(path):
  date_filename = datetime.strptime(filename.split(" ")[0], '%Y_%m_%d_%H_%M_%S')
  if date_filename &lt; datetime.now() - datetime.timedelta(days=days_to_subtract):
    if fnmatch.fnmatch(filename, pattern):
      matchingfiles.append(os.path.join(path, filename)) # put all matching filenames in array
      # if you just want to include the filename, you can do this instead:
      # matchingfiles.append(filename)

print matchingfiles
</code></pre>

<p>No need to reimport datetime, you can simply use <code>datetime.strptime</code> instead of <code>datetime.datetime.strptime</code>. And put all imports at the top the file, it makes it MUCH easier to read and maintain the code.</p>

python arrays list