我正在尝试创建一个脚本,该脚本将遍历rootDir
的所有文件夹和子文件夹,以查找特定的文件夹和文件集。如果脚本将在其中找到文件夹(例如testfolder1
):
textfile.txt
image.jpg
subtitles.dxfp
testsubfolder1
文件的另一个文件夹(例如video.mp4
)testsubfolder2
文件的文件夹(例如video_trailer.mp4
)它将创建包含textfile.txt
,image.jpg
,subtitles.dxfp
(如果找到),video.mp4
和video_trailer.mp4
(如果找到)的存档,并将其保存在rootDir中。
[当前,我有一段代码以递归方式遍历所有这些文件,但是其中不包括video.mp4
和video_trailer.mp4
在文件夹中。我应该如何修改我的代码以达到想要的效果?我猜应该从头看是否找到了textfile.txt
,image.jpg
和subtitles.dxfp
,如果是这样,它将查找是否存在包含video.mp4
文件的文件夹,但不是递归地查找到末尾,它会搜索包含video_trailer.mp4
文件。我对吗?我不知道如何正确编写代码。预先感谢您提供任何使我更接近解决方案的技巧。
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 / -type f -name "*.jpg" -exec tar -czf /tmp/jpg.tar.gz {} \;
使用-u,您可以更新现有档案。
问候,福克斯