我怎样才能只保留这.txt
两个文件夹中的文件imgur/images/vase
以及与花瓶图像文件夹中的文件imgur/comments/vase
相关的文件?.jpeg
mona@pascal:~/computer_vision/imgur/images$ ls vase/
09PcZOp.jpeg aAk8xnc.jpeg BmlEmqV.jpeg fYJdYsw.jpeg JLlgK86.jpeg P0EqiL6.jpeg r5cwq2Z.jpeg uYQdAkX.jpeg wZvDVUV.jpeg YOjY83l.jpeg
5bSmL.jpeg aepd0Fv.gif CXENf98.jpeg GWZNImv.jpeg NDKCSnv.jpeg P6UcA4a.jpeg t1Pvv.jpeg VEgOghz.jpeg xaGoY.jpeg ZDVSteW.png
7IIMF9n.jpeg B6L0ZWn.gif ee8uEPA.png HUZLjjR.jpeg oUwwY47.jpeg Qcr4c2x.jpeg TurgWyF.jpeg VpcMGTP.gif YIbyIp7.jpeg zeTwDul.png
7Mk599E.jpeg bL9B5Ej.jpeg eGFLBGj.jpeg IYepKMc.jpeg oUXgilJ.png R4EdALX.png UmzKe2R.gif wK7EjUG.gif YImIVh4.jpeg
mona@pascal:~/computer_vision/imgur/images$ ls ../comments/vase/
09PcZOp.txt 7Mk599E.txt B6L0ZWn.txt CXENf98.txt fYJdYsw.txt IYepKMc.txt oUwwY47.txt P6UcA4a.txt r5cwq2Z.txt UmzKe2R.txt VpcMGTP.txt xaGoY.txt YOjY83l.txt
5bSmL.txt aAk8xnc.txt bL9B5Ej.txt ee8uEPA.txt GWZNImv.txt JLlgK86.txt oUXgilJ.txt Qcr4c2x.txt t1Pvv.txt uYQdAkX.txt wK7EjUG.txt YIbyIp7.txt ZDVSteW.txt
7IIMF9n.txt aepd0Fv.txt BmlEmqV.txt eGFLBGj.txt HUZLjjR.txt NDKCSnv.txt P0EqiL6.txt R4EdALX.txt TurgWyF.txt VEgOghz.txt wZvDVUV.txt YImIVh4.txt zeTwDul.txt
mona@pascal:~/computer_vision/imgur/images/vase$ ls *jpeg
09PcZOp.jpeg 7Mk599E.jpeg BmlEmqV.jpeg fYJdYsw.jpeg IYepKMc.jpeg oUwwY47.jpeg Qcr4c2x.jpeg TurgWyF.jpeg wZvDVUV.jpeg YImIVh4.jpeg
5bSmL.jpeg aAk8xnc.jpeg CXENf98.jpeg GWZNImv.jpeg JLlgK86.jpeg P0EqiL6.jpeg r5cwq2Z.jpeg uYQdAkX.jpeg xaGoY.jpeg YOjY83l.jpeg
7IIMF9n.jpeg bL9B5Ej.jpeg eGFLBGj.jpeg HUZLjjR.jpeg NDKCSnv.jpeg P6UcA4a.jpeg t1Pvv.jpeg VEgOghz.jpeg YIbyIp7.jpeg
例如我最终想要得到:
09PcZOp.txt 7Mk599E.txt BmlEmqV.txt fYJdYsw.txt IYepKMc.txt oUwwY47.txt Qcr4c2x.txt TurgWyF.txt wZvDVUV.txt YImIVh4.txt
5bSmL.txt aAk8xnc.txt CXENf98.txt GWZNImv.txt JLlgK86.txt P0EqiL6.txt r5cwq2Z.txt uYQdAkX.txt xaGoY.txt YOjY83l.txt
7IIMF9n.txt bL9B5Ej.txt eGFLBGj.txt HUZLjjR.txt NDKCSnv.txt P6UcA4a.txt t1Pvv.txt VEgOghz.txt YIbyIp7.txt
留在 imgur/comments 文件夹中。
此外我还有以下所有这些文件夹:
mona@pascal:~/computer_vision/imgur/images$ ls
aeroplane baseball glove broccoli cell phone dining table frisbee keyboards motorcycles plants skateboard teddy bear trains
airplane bear bus chair dog giraffe kite mouse refrigerator ski tennis racket truck
airplanes bears cake chairs dogs giraffes knife orange remote skis tie trucks
apple bench cakes clock donut handbag laptop oranges sandwich snowboard toaster tv
apples bird car clocks doughnut horse laptops oven sandwiches spoon toilet umbrella
backpack birds carrot couch elephant horses microwave parking meter scissors sports ball toilets vase
banana book cars cow elephants hotdog motorbike person sheep stop sign toothbrush wine glass
bananas books cat cows fire hydrant hotdogs motorbikes pizza sink suitcase traffic light zebra
baseball bat bowl cats cup fork keyboard motorcycle plant sinks surfboard train zebras
答案1
这个 Python 脚本完成了这个工作:
import os.path as osp
import os
import re
for directory in os.listdir('images'):
files = [f for f in os.listdir('images/'+directory+'/') if not re.match(r'([a-za-z]|[0-9])+.*\.jpeg', f)]
for f in files:
complete_file_path = 'comments/'+ directory+'/'+f[:-4]+'.txt'
if osp.exists(complete_file_path):
os.remove(complete_file_path)