简体   繁体   中英

How can I search for specific part of strings in a list and list them? Python 2.7

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 ie 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:

## 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 < 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. []

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 (eg matchingfiles):

## 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 < 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM