python递归搜索文件再同步到服务器,Python - 搜索文件,递归地匹配目录中的给定名称...

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

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值