walk()期间的fnmatch不断产生错误的文件位置

时间:2016-07-06 07:00:47

标签: python python-2.7

我一直在绞尽脑汁想弄清楚为什么fnmatch()在这个循环中不起作用。每次我运行它时,它都说我传递的参数的文件在我遍历的每个目录中。我无法弄清楚原因。我重写了整个应用程序,只是为了遇到同样的行为。任何人都知道为什么会发生这种情况?

import os
import argparse
import fnmatch

parser = argparse.ArgumentParser(description="""This application takes a string and navigates the file system looking
                                         for either a file or directory with the name of the string that was
                                         passed. The output should be the absolute path (e.g. full path) to the
                                         directory or file.""")
parser.add_argument('Root',
                type = str,
                help = 'This is the root directory to start looking in. This is required.',
                )
parser.add_argument('String',
                type=str,
                help = 'This is the string to use to search for file or directory. This is required.',
                )
parser.add_argument('-f', '--file',
                help = 'This will force the program to only list files. This is optional.',
                action = 'store_true',
                )
parser.add_argument('-d', '--directory',
                help = 'This will force the program to only list directories. This is optional.',
                action = 'store_true',
                )
parser.add_argument('-m', '--last-modified',
                help='The time the file/directory was last modified. This is optional.',
                action = 'store_true')

if __name__ == '__main__':
    arguments = parser.parse_args(['/Users/marco/Dropbox/School/Python', 'testfile'])

else:
    arguments = parser.parse_args()

root_directory = arguments.Root
search_string = arguments.String

for root, dirs, files in os.walk(root_directory):
    for dir in dirs:
        for file in files:
            if fnmatch.fnmatch(file, search_string + ".*"):
                print "File found at: " + os.path.join(root, dir, file)

文件匹配" testfile"仅存在于以下3个文件夹中,但对于存在的每个文件夹,都会打印错误的文件名。

File found at: /Users/marco/Dropbox/School/Python/.idea/testfile.txt
File found at: /Users/marco/Dropbox/School/Python/Argument Passing/testfile.txt
File found at: /Users/marco/Dropbox/School/Python/Assignment 1/testfile.txt
File found at: /Users/marco/Dropbox/School/Python/Assignment 2/testfile.txt
File found at: /Users/marco/Dropbox/School/Python/Assignment 3/testfile.txt
File found at: /Users/marco/Dropbox/School/Python/Assignment 4/testfile.txt
File found at: /Users/marco/Dropbox/School/Python/ClassExample/testfile.txt
File found at: /Users/marco/Dropbox/School/Python/dummy/testfile.txt
File found at: /Users/marco/Dropbox/School/Python/file.system/testfile.txt
File found at: /Users/marco/Dropbox/School/Python/Hangman/testfile.txt
File found at: /Users/marco/Dropbox/School/Python/HierarchyDemo/testfile.txt
File found at: /Users/marco/Dropbox/School/Python/InheritanceExample/testfile.txt
File found at: /Users/marco/Dropbox/School/Python/prob2/testfile.txt
File found at: /Users/marco/Dropbox/School/Python/Quiz 4/testfile.txt
File found at: /Users/marco/Dropbox/School/Python/Rosalind/testfile.txt
File found at: /Users/marco/Dropbox/School/Python/Quiz 4/testfile/testfile.txt

0 个答案:

没有答案