[英]Scripts traversing through directories looking for specific set of files and folders
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.我正在尝试创建一个脚本,该脚本将遍历
rootDir
所有文件夹和子文件夹, rootDir
查找特定的文件夹和文件集。 If script will find the folder (for ex. testfolder1
) in which there are:如果脚本会找到文件夹(例如
testfolder1
),其中有:
textfile.txt
image.jpg
subtitles.dxfp
subtitles.dxfp
testsubfolder1
) containing video.mp4
filevideo.mp4
文件的文件夹(例如testsubfolder1
)testsubfolder2
) containing video_trailer.mp4
file video_trailer.mp4
文件的另一个文件夹(例如testsubfolder2
) 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.它将创建包含
textfile.txt
、 image.jpg
、 subtitles.dxfp
(如果找到)、 video.mp4
和video_trailer.mp4
(如果找到)的存档并将其保存在 rootDir 中。
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.目前我有递归遍历所有这些文件的片段,但它不包括文件夹中的
video.mp4
和video_trailer.mp4
。 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.如果找到
textfile.txt
、 image.jpg
和subtitles.dxfp
,我想它应该查看开头,如果是这样,它会查看是否存在包含video.mp4
文件的文件夹,但不是递归的,最后它会搜索另一个包含video.mp4
文件的文件夹video_trailer.mp4
文件。 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()
what about using find?使用查找怎么样?
find / -type f -name "*.jpg" -exec tar -czf /tmp/jpg.tar.gz {} \;
with -u you can update existing archives.使用 -u 您可以更新现有档案。
Greetings, fuchs问候,福克斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.