程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

An interesting interview question for a factory test, can you answer it with Python or Shell?

編輯:Python

This article is a test development engineerVennAn interview question encountered by a classmate in an interview with a famous Internet company,首發於Testerhome社區,sparked interesting discussions and answers,For the reference of all technical friends.

Interesting test questions asked in an interview with JD Finance

題目:在A文件夾下有多個子文件夾(a1、b1、c1),There are several in each subfolderjpg圖片,要求寫一段代碼(用Python or Shell),Copy all these pictures and save themB文件夾下.

A discussion of a small group of test engineers

聰明的Cookie同學:The test point is how to traverse the files under a folder,Something to consider is file path depth,需要用到遞歸.

Honest Montenegrin old demon classmate:我覺得對我來說,The difficulty is the method of manipulating the file,之前沒怎麼用過,Recursive traversal is a small problem.

Experienced students who cut candles:If the interview with this subject test engineer,You definitely need to ask this(Test your needs analysis),More than just writing a script,等你寫完了(Test your programming familiarity),will also let you test against the code you write(Test your case design),都是套路.

爆炸的hellohell同學:我再想,if i run into this problem,Can you give the correct answer on the spot??Probably not,因為API全忘掉了.really bad memory.If you give me a book,give the internet a chance,take more time,can do it;even used recursion,生成器,精簡了代碼(usurped into a line),做了判斷.
jpgwhat about a directory?What to do if the files in different directories have the same name?以及其他
But only if you write code without referencing anything.But in practice, it seems that there are not many such people.;so,I can only in situ exploded.However, I still find it usefulShellBetter to solve this problem.

參考答案

Python解答

# -*- 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解答

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解答

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

加入我們

進群交流、獲取更多干貨, 請關注微信公眾號:


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved