查找名称为test的文件

Linux Shell,

find  . -name "test" -type f

Java程序,

package com.oracle.test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.testng.annotations.Test;

public class FindFileTest {
    
    public void findFile(String rootDir, List<String> files) {
        
        File f = new File(rootDir);
        
        if(f.isFile() && f.getName().equals("test")) {
            files.add(f.getAbsolutePath());
        }
        
        File[] fSubs = f.listFiles();
        
        if(fSubs == null || fSubs.length == 0) {
            return;
        }
            
        for(File fSub: fSubs) {
            findFile(fSub.getAbsolutePath(), files);
        }
    }
    
    
    @Test
    void testFindFile() {
        String rootDir = System.getProperty("user.dir");
        List<String> files = new ArrayList<String>();
        findFile(rootDir, files);
        System.out.println(files);
    }
}

程序输出


Screen Shot 2019-04-26 at 6.50.15 AM.png

Python程序,

# coding:utf-8
import os
def findfile(dir,ls=[]):
    child_dirs = os.listdir(dir)
    for child_dir in child_dirs:
        child_dir = dir + os.sep + child_dir
        if os.path.isfile(child_dir) and os.path.basename(child_dir) == 'test':
            ls.append(os.path.abspath(child_dir))
        elif os.path.isdir(child_dir):
            findfile(child_dir, ls)



if __name__ == '__main__':
    pwd = os.getcwd()
    ls = []
    findfile(pwd, ls)
    print(ls)

程序输出,


Screen Shot 2019-04-26 at 7.21.26 AM.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 5,926评论 0 10
  • 摘自 IBM2003 年 11 月 10 日发布 链接 关于本词汇表 对于初涉 Linux 世界的 Microso...
    雲水天長阅读 965评论 1 4
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,736评论 0 3
  • 那么就精心准备一次聚餐。 叫上那些老朋友,一起去采购食物。会有水果,做几盘水果沙拉;会有饺子皮和饺子馅,一起包饺子...
    l也猫阅读 215评论 0 0
  • 文/初漪 去地铁站的路上,总会遇见一位瘦瘦小小的环卫工人在清扫人行道上的落叶,他穿着鲜艳醒目的环卫服,我远远就能看...
    初漪阅读 517评论 3 7