通过文件名循环超过必要的

时间:2013-05-14 07:41:00

标签: python

我有以下代码:

if inputFileName: 
    if inputFileName.lower().endswith(mediaExt):
        for word in ignoreWords:
            if word not in inputFileName.lower():
                if os.path.isfile(inputDirectory):
                    try:
                        processFile(fileAction, inputDirectory, outputDestination)
                    except Exception, e:
                        logging.error(loggerHeader + "There was an error when trying to process file: %s", os.path.join(inputDirectory, inputFileName))
                        logging.exception(e)
                else:
                    try:
                        processFile(fileAction, os.path.join(inputDirectory, inputFileName), outputDestination)
                    except Exception, e:
                        logging.error(loggerHeader + "There was an error when trying to process file: %s", os.path.join(inputDirectory, inputFileName))
                        logging.exception(e)

ignoreWords是一个包含几个单词的列表,我不想让文件名包含。现在我的问题是这将循环遍历列表中x项的同一文件。我希望它只匹配一次单词(或者在完成匹配时运行一次processFile)但不能完全找到合适的解决方案

2 个答案:

答案 0 :(得分:1)

替换

for word in ignoreWords:
    if word not in inputFileName.lower():

if not any(word in inputFileName.lower() for word in ignoreWords):

答案 1 :(得分:0)

你应该循环文件名。如果文件名不在您的ignoreWords列表中,则会丢弃它。

      if inputFileName: 
            if inputFileName.lower().endswith(mediaExt):
                for word in inputFileName.lower():
                    if word not in ignoreList:
                        if os.path.isfile(inputDirectory):
                            try:
                                processFile(fileAction, inputDirectory, outputDestination)
                            except Exception, e:
                                logging.error(loggerHeader + "There was an error when trying to process file: %s", os.path.join(inputDirectory, inputFileName))
                                logging.exception(e)
                        else:
                            try:
                                processFile(fileAction, os.path.join(inputDirectory, inputFileName), outputDestination)
                            except Exception, e:
                                logging.error(loggerHeader + "There was an error when trying to process file: %s", os.path.join(inputDirectory, inputFileName))
                                logging.exception(e)