一道有趣的大厂测试面试题,你能用 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. def movefile(srcfile,dstfile):

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

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

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

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

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

  9. else:

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

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

  12. os.makedirs(fpath)

  13. shutil.move(srcfile,dstfile)

  14. def getfile(path):

  15. paths = []

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

  17. for file in files:

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

  19. return paths

  20. def main():

  21. path = "/path/A"

  22. pathto = "/path/B"

  23. paths = getfile(path)

  24. for pathfrom in paths:

  25. print(pathfrom)

  26. movefile(pathfrom,pathto)

  27. if __name__ == '__main__':

  28. main()

Java 解答(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. if(from.isFile()) {

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

  7. }

  8. if(to.isFile()) {

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

  10. }

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

  12. @Override

  13. public boolean accept(File pathname) {

  14. boolean result = false;

  15. if(pathname.isFile()) {

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

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

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

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

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

  21. result = true;

  22. }

  23. } else {

  24. result = false;

  25. }

  26. return result;

  27. }

  28. });

  29. for(File image : images) {

  30. copyImagesHelper(image, to);

  31. }

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

  33. @Override

  34. public boolean accept(File pathname) {

  35. return pathname.isDirectory();

  36. }

  37. });

  38. for(File dir : dirs) {

  39. copyImages(from, to);

  40. }

  41. }

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

  43. String cmd =

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

  45. Runtime runtime = Runtime.getRuntime();

  46. runtime.exec(cmd);

  47. }

Shell 解答(by 杰)

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

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

。(end)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值