遍历目录查找特定文件和文件夹集的脚本 [英] Scripts traversing through directories looking for specific set of files and folders

查看:35
本文介绍了遍历目录查找特定文件和文件夹集的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个脚本,该脚本将遍历 rootDir 的所有文件夹和子文件夹,以查找特定的文件夹和文件集.如果脚本会找到文件夹(例如 testfolder1),其中有:

I'm trying to create a script that will traverse through all folders and subfolders of rootDir looking for specific set of folders and files. If script will find the folder (for ex. testfolder1) in which there are:

  • textfile.txt
  • image.jpg
  • (可选)subtitles.dxfp
  • 另一个包含 video.mp4 文件的文件夹(例如 testsubfolder1)
  • (可选)另一个包含 video_trailer.mp4 文件的文件夹(例如 testsubfolder2)
  • textfile.txt
  • image.jpg
  • (optionally) subtitles.dxfp
  • another folder (ex. testsubfolder1) containing video.mp4 file
  • (optionally) another folder (ex. testsubfolder2) containing video_trailer.mp4 file

它将创建包含 textfile.txtimage.jpgsubtitles.dxfp(如果找到的话)、video.mp4video_trailer.mp4(如果找到)并将其保存在 rootDir 中.

it will create archive containing textfile.txt, image.jpg, subtitles.dxfp(if they were in found), video.mp4 and video_trailer.mp4 (if it was found) and save it in rootDir.

目前我有递归遍历所有这些文件的片段,但它不包括 video.mp4video_trailer.mp4 在文件夹中.我应该如何修改我的代码以达到想要的效果?如果找到 textfile.txtimage.jpgsubtitles.dxfp,我想它应该查看开头,如果找到,则查看是否存在存在包含 video.mp4 文件的文件夹,但不是递归的,最后它会搜索另一个包含 video_trailer.mp4 文件的文件夹.我对吗?我不知道我应该如何在代码中正确编写它.提前感谢您提供的任何提示,让我更接近解决方案.

Currently I have snippet that traverse recursively looking for all those files, but it's not including that video.mp4 and video_trailer.mp4 are in folders. How should I modify my code in order to achieve wanted effect? I guess it should look at the beginning if textfile.txt, image.jpg and subtitles.dxfp were found, if so it looks if there exist folder containing video.mp4 file, but not recursively and at the end it searches for another folder containing video_trailer.mp4 file. Am i right? I do not know how should i properly write it in code. Thank you in advance for any tips bringing me closer to the solution.

for dirpath, dirnames, filenames in os.walk(rootDir):
    jpg = glob.glob(os.path.join(rootDir, dirpath, '*.jpg'))
    mp4 = glob.glob(os.path.join(rootDir, dirpath, '*.mp4'))
    txt = glob.glob(os.path.join(rootDir, dirpath, '*.txt'))
    xml = glob.glob(os.path.join(rootDir, dirpath, '*.xml'))
    dxfp = glob.glob(os.path.join(rootDir, dirpath, '*.dxfp'))

    if jpg and mp4 and txt:
        if xml and dxfp:
            #Archive will have the same name as image
            tarName  = [i for i in filenames if ".jpg" in i] 
            tar = tarfile.open("{0}.tar".format(tarName[0].replace(".jpg","")), "w")

            for file in [jpg, mp4, txt, xml, dxfp]:
                tar.add(file[0])
            tar.close()
        else:
            tarName  = [i for i in filenames if ".jpg" in i] 
            tar = tarfile.open("{0}.tar".format(tarName[0].replace(".jpg","")), "w")
            for file in [jpg, mp4, txt]:
                tar.add(file[0])
            tar.close()

推荐答案

使用 find 怎么样?

what about using find?

find / -type f -name "*.jpg" -exec tar -czf /tmp/jpg.tar.gz {} \;

使用 -u 您可以更新现有档案.

with -u you can update existing archives.

你好,朋友们

这篇关于遍历目录查找特定文件和文件夹集的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆