霍格沃兹测试开发学社

《Python测试开发进阶训练营》(随到随学!)
2023年第2期《Python全栈开发与自动化测试班》(开班在即)
报名联系weixin/qq:2314507862

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

一道有趣的测试面试题目

题目:

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

一小撮测试工程师的讨论

聪明的 Cookie 同学:考点就是如何遍历一个文件夹下的文件,需要考虑的是文件路径深度,需要用到递归。

** **诚实的 黑山老妖同学:我觉得对我来说,难点是操作文件的方法,之前没怎么用过,递归遍历啥的倒是小问题。

经验老道的 剪烛
同学:如果拿这个题目面试测试工程师,这个肯定还需要你提问的(考你需求分析),不仅仅是说写个脚本,等你写完了(考你编程熟悉),还会让你针对你写的代码进行测试(考你用例设计),都是套路。

爆炸的 hellohell 同学:我再想,如果我碰到这个问题,是否能当场给出正确答案?估计不成,因为 API
全忘掉了。确实记性不好。如果给我个本儿,给上网机会,多费点时间,能搞出来;甚至用了递归,生成器,精简了代码(篡成一行),做了判断。

  • jpg 是个目录咋办?

  • 不同目录下文件同名咋办?

  • 以及其他

但前提是你不参考任何东西就写代码。但实际工作中好像这种人不多;so, 我只能原地爆炸了。 不过打心里还是觉得用 Shell 解决这个问题比较好些。

参考答案

Python 解答一(by 煎饼果子)

      1.  # -*- coding: utf-8 -*-

  2. import os,shutil

  3.   


  4. def movefile(srcfile,dstfile):

  5.     fpath,fname=os.path.split(srcfile)

  6.     if os.path.isfile(os.path.join(dstfile,fname)):

  7.         print("%s exist!"%str(os.path.join(dstfile,fname)))

  8.     elif not os.path.isfile(srcfile):

  9.         print("%s not exist!")%(srcfile)

  10.     else:

  11.         fpath,fname=os.path.split(dstfile)

  12.         if not os.path.exists(fpath):

  13.             os.makedirs(fpath)

  14.         shutil.move(srcfile,dstfile)

  15.   


  16. def getfile(path):

  17.     paths = []

  18.     for root, dirs, files in os.walk(path):

  19.         for file in files:

  20.             paths.append(os.path.join(root,file))

  21.     return paths

  22.   


  23. def main():

  24.     path = "/path/A"

  25.     pathto = "/path/B"

  26.     paths = getfile(path)

  27.     for pathfrom in paths:

  28.         print(pathfrom)

  29.         movefile(pathfrom,pathto)

  30.   


  31. if __name__ == '__main__':

  32.     main()

Python 解答一(by Lucas)

      1.  public void copyImages(File from, File to) throws IOException {

  2.     if(from == null || to == null) {

  3.         throw new RuntimeException("From or To is empty.");

  4.     }

  5.   


  6.     if(from.isFile()) {

  7.         throw new RuntimeException("From is not directory.");

  8.     }

  9.   


  10.     if(to.isFile()) {

  11.         throw new RuntimeException("To is not directory.");

  12.     }

  13.   


  14.     File[] images = from.listFiles(new FileFilter() {

  15.         @Override

  16.         public boolean accept(File pathname) {

  17.             boolean result = false;

  18.             if(pathname.isFile()) {

  19.                 String path = pathname.getAbsolutePath().toLowerCase();

  20.                 if(path.lastIndexOf(".jpg") > -1

  21.                     || path.lastIndexOf(".png") > -1

  22.                     || path.lastIndexOf(".jpeg") > -1

  23.                     || path.lastIndexOf(".bmp") > -1) {

  24.   


  25.                     result = true;

  26.                 }

  27.             } else {

  28.                 result = false;

  29.             }

  30.             return result;

  31.         }

  32.     });

  33.   


  34.     for(File image : images) {

  35.         copyImagesHelper(image, to);

  36.     }

  37.   


  38.     File[] dirs = from.listFiles(new FileFilter() {

  39.         @Override

  40.         public boolean accept(File pathname) {

  41.             return pathname.isDirectory();

  42.         }

  43.     });

  44.   


  45.     for(File dir : dirs) {

  46.         copyImages(from, to);

  47.     }

  48. }

  49.   


  50. private void copyImagesHelper(File image, File dir) throws IOException {

  51.     String cmd =

  52.         String.format("cp %s %s", image.getAbsolutePath(), dir.getAbsolutePath());

  53.     Runtime runtime = Runtime.getRuntime();

  54.     runtime.exec(cmd);

  55. }

Shell 解答(by 杰)

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

P.S. 以上答案仅供参考,欢迎大家在留言区,回复你的精彩解答,也许有惊喜哦 。(end)

-

来霍格沃兹测试开发学社,学习更多软件测试与测试开发的进阶技术,知识点涵盖web自动化测试 app自动化测试、接口自动化测试、测试框架、性能测试、安全测试、持续集成/持续交付/DevOps,测试左移、测试右移、精准测试、测试平台开发、测试管理等内容,课程技术涵盖bash、pytest、junit、selenium、appium、postman、requests、httprunner、jmeter、jenkins、docker、k8s、elk、sonarqube、jacoco、jvm-sandbox等相关技术,全面提升测试开发工程师的技术实力
QQ交流群:484590337
公众号 TestingStudio
点击获取更多信息

posted @ 2022-01-10 09:09  霍格沃兹测试开发学社  阅读(51)  评论(0编辑  收藏  举报