• 一道有趣的大厂测试面试题,你能用 Python or Shell 解答吗?


    ⬇️ 点击“下方链接”,提升测试核心竞争力!

    >>更多技术文章分享和免费资料领取原文链接
    本文是测试开发工程师Venn同学面试某互联网名企遇到的一道面试题目,首发于Testerhome社区,引发了有趣的讨论和解答,供各位测试同学参考。链接:https://testerhome.com/topics/18337

    一道有趣的测试面试题目

    题目:在A文件夹下有多个子文件夹(a1、b1、c1),每个子文件夹下有好几张jpg图片,要求写一段代码(用PythonorShell),把这些图片全部拷贝并存在B文件夹下。

    一小撮测试工程师的讨论

    聪明的Cookie同学:考点就是如何遍历一个文件夹下的文件,需要考虑的是文件路径深度,需要用到递归。
    诚实的黑山老妖同学:我觉得对我来说,难点是操作文件的方法,之前没怎么用过,递归遍历啥的倒是小问题。
    经验老道的剪烛同学:如果拿这个题目面试测试工程师,这个肯定还需要你提问的(考你需求分析),不仅仅是说写个脚本,等你写完了(考你编程熟悉),还会让你针对你写的代码进行测试(考你用例设计),都是套路。

    爆炸的hellohell同学:我再想,如果我碰到这个问题,是否能当场给出正确答案?估计不成,因为API全忘掉了。确实记性不好。如果给我个本儿,给上网机会,多费点时间,能搞出来;甚至用了递归,生成器,精简了代码(篡成一行),做了判断。
    jpg是个目录咋办?不同目录下文件同名咋办?以及其他
    但前提是你不参考任何东西就写代码。但实际工作中好像这种人不多;so,我只能原地爆炸了。不过打心里还是觉得用Shell解决这个问题比较好些。

    参考答案

    Python解答(by煎饼果子)

     # -*- coding: utf-8 -*-
    import os,shutil
    def movefile(srcfile,dstfile):
        fpath,fname=os.path.split(srcfile)
        if os.path.isfile(os.path.join(dstfile,fname)):
            print("%s exist!"%str(os.path.join(dstfile,fname)))
        elif not os.path.isfile(srcfile):
            print("%s not exist!")%(srcfile)
        else:
            fpath,fname=os.path.split(dstfile)
            if not os.path.exists(fpath):
                os.makedirs(fpath)
            shutil.move(srcfile,dstfile)
    def getfile(path):
        paths = []
        for root, dirs, files in os.walk(path):
            for file in files:
                paths.append(os.path.join(root,file))
        return paths
    def main():
        path = "/path/A"
        pathto = "/path/B"
        paths = getfile(path)
        for pathfrom in paths:
            print(pathfrom)
            movefile(pathfrom,pathto)
    if __name__ == '__main__':
        main() 
    

    Java解答(byLucas)

     public void copyImages(File from, File to) throws IOException {
        if(from == null || to == null) {
            throw new RuntimeException("From or To is empty.");
        }
        if(from.isFile()) {
            throw new RuntimeException("From is not directory.");
        }
        if(to.isFile()) {
            throw new RuntimeException("To is not directory.");
        }
        File[] images = from.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                boolean result = false;
                if(pathname.isFile()) {
                    String path = pathname.getAbsolutePath().toLowerCase();
                    if(path.lastIndexOf(".jpg") > -1
                        || path.lastIndexOf(".png") > -1
                        || path.lastIndexOf(".jpeg") > -1
                        || path.lastIndexOf(".bmp") > -1) {
                        result = true;
                    }
                } else {
                    result = false;
                }
                return result;
            }
        });
        for(File image : images) {
            copyImagesHelper(image, to);
        }
        File[] dirs = from.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return pathname.isDirectory();
            }
        });
        for(File dir : dirs) {
            copyImages(from, to);
        }
    }
    private void copyImagesHelper(File image, File dir) throws IOException {
        String cmd =
            String.format("cp %s %s", image.getAbsolutePath(), dir.getAbsolutePath());
        Runtime runtime = Runtime.getRuntime();
        runtime.exec(cmd);
    } 
    

    Shell解答(by杰)

     find  ./A/ -maxdepth 2  -name '*.jpg' -exec cp {} ./B \; 
    

    P.S.以上答案仅供参考,欢迎大家在留言区,回复你的精彩解答,也许有惊喜哦。(end)### ⬇️ 点击“下方链接”,提升测试核心竞争力!
    >>更多技术文章分享和免费资料领取
    img

  • 相关阅读:
    java实现快速排序
    java实现二叉树
    hudson——持续集成
    Oracle存储过程的数组参数
    在linux系统下建立artifactory管理maven库
    关于排错:专注思考,细心观察,步步为营
    在关键字'('附近有语法错误 Incorrect syntax near '(' in sql server table values function
    快速将PSD文件生成WordPress主题模板Divine
    SQLserver 复制分发( 发布与订阅) 疑难杂症 Replication (Publications,Subscriptions)
    Windows8将撼动笔记本电脑市场?
  • 原文地址:https://www.cnblogs.com/hogwarts/p/16148058.html
Copyright © 2020-2023  润新知