Python - 搜索文件,递归地匹配目录中的给定名称

时间:2016-01-20 10:44:22

标签: python-3.x

我正在尝试制作一个脚本来递归搜索给定目录的文件(给定名称),格式如下:

pi@pi ~ $ ./lect4_first.py /home dranie

这是完整的脚本:

#!/usr/bin/python3

import sys
import os
flag = False

dir_path, file_name = sys.argv[1], sys.argv[2]

for root, directories, filenames in os.walk(dir_path):
  if file_name.startswith("*") or file_name.endswith("*"):
    for filename in filenames:
      if str(filename).endswith(file_name[1:]):
        flag = True
        print(os.path.join(root, filename))
      elif str(filename).startswith(file_name[:-2]):
        flag = True
        print(os.path.join(root, filename))
  else:
    for filename in filenames:
      if str(filename) == file_name:
        flag = True
        print(os.path.join(root, filename))

if flag == False:
    print("'{}' not found in the '{}' directory".format(file_name, dir_path))

到目前为止,脚本可以正常工作,但如果找不到匹配项 - 它会打印多条未找到的消息:

dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home
dranie not found in /home

如果在目录或其子目录下找不到具有给定名称的文件,我想只打印一条未找到的消息,但到目前为止我无法弄清楚...

1 个答案:

答案 0 :(得分:1)

我不知道" lect4_first.py"包含?

你可以试试这个:

import os
import sys
args = sys.argv
if len(args) == 3:
    directory,filename = args[1],args[2]
    for root, dirs, files in os.walk(directory):
        if filename in files:
           f_name = os.path.join(root, filename)
           print "File exists and its path is : "+f_name
           break
    else:
        print "File does not exist"

输出:

root:~/learn/python# python file_exist.py /root test_file_exists_1.txt
File does not exist
root:~/learn/python# python file_exist.py /root test_file_exists.txt
File exists and its path is : /root/learn/python/test_file_exists.txt