1

我有以下代码:

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)但不太能够找到合适的解决方案

4

2 回答 2

1

代替

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

if not any(word in inputFileName.lower() for word in ignoreWords):
于 2013-05-14T07:42:24.723 回答
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)
于 2013-05-14T08:29:36.837 回答