如何获取字符串列表并查找名称与列表中的字符串匹配的文件?

我有一个包含 600 多个数字的列表,以及包含 50,000 多个文件的目录。所有文件的命名如下:


99574404682_0.jpg

99574404682_1.jpg

99574437307_0.gif

99574437307_1.gif

99574437307_2.gif

99574449752.jpg

99574457597.jpg

99581722007.gif

我想复制名称与列表中的数字匹配的任何文件,直到下划线,然后复制到新目录。


例如,如果我的列表包含:


99574404682

99574449752

99581722007

然后是文件:


99574404682_0.jpg

99574404682_1.jpg

99574449752.jpg

99581722007.gif

将被复制到一个新目录。我在使用 bash 3.2 的 Mac 上。我在想像 python 这样的东西是我需要使用的,因为列表对于 grep 或 find 来说太大了,但我不确定。谢谢!


桃花长相依
浏览 179回答 3
3回答

慕娘9325324

在python中使用os模块和shutil模块import osimport shutil你可以准备一个包含匹配模式喜欢的列表match_pattern=['99574404682','99574449752','99581722007']然后使用 os.listdir() 获取包含源目录中文件名的列表files_in_source_dir=os.listdir(source_directory_path)最后复制匹配的文件for file in files_in_source_dir:  if file.split('.')[0] in match_pattern: #using split('.')[0] to get filename without extend name    shutil.copyfile(source_directory_path+file,target_directory_path+file)

喵喵时光机

您可以使用shutil.copy()将文件从源复制到目标。from shutil import copyfrom os import listdirfrom os import makedirsfrom os.path import abspathfrom os.path import existsfrom os.path import splitextfilenames = {'99574404682', '99574449752', '99581722007'}src_path = # your filesdest_path = # where you want to put them# make the destination if it doesn't existif not exists(dest_path):&nbsp; &nbsp; makedirs(dest_path)# go over each file in src_pathfor file in listdir(src_path):&nbsp; &nbsp; # If underscore in file&nbsp; &nbsp; if "_" in file:&nbsp; &nbsp; &nbsp; &nbsp; prefix, *_ = file.split("_")&nbsp; &nbsp; # otherwise treat as normal file&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; prefix, _ = splitext(file)&nbsp; &nbsp; # only copy if prefix exist in above set&nbsp; &nbsp; if prefix in filenames:&nbsp; &nbsp; &nbsp; &nbsp; copy(abspath(file), dest_path)这会导致以下文件dest_path:99574404682_0.jpg&nbsp;&nbsp;99574404682_1.jpg&nbsp;&nbsp;99574449752.jpg&nbsp;&nbsp;99581722007.gif我不是真正的 bash 专家,但你可以尝试这样的事情:#!/bin/bashdeclare -a arr=("99574404682" "99574449752" "99581722007")## Example directories, you can change thesesrc_path="$PWD/*"dest_path="$PWD/src"if [ ! -d "$dest_path" ]; then&nbsp; &nbsp; mkdir $dest_pathfifor f1 in $src_path; do&nbsp;&nbsp; &nbsp; filename=$(basename $f1)&nbsp; &nbsp; prefix="${filename%.*}"&nbsp; &nbsp; IFS='_' read -r -a array <<< $prefix&nbsp; &nbsp; for f2 in "${arr[@]}"; do&nbsp; &nbsp; &nbsp; &nbsp; if [ "${array[0]}" == "$f2" ]; then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cp $f1 $dest_path&nbsp; &nbsp; &nbsp; &nbsp; fi&nbsp; &nbsp; donedone

慕沐林林

您可以遍历两个列表,根据startswith条件从一个列表中获取项目:files_lst = ['99574404682_0.jpg', '99574404682_1.jpg', '99574437307_0.gif', '99574437307_1.gif', '99574437307_2.gif', '99574449752.jpg', '99574457597.jpg', '99581722007.gif']lst = [99574404682, 99574449752, 99581722007]for x in files_lst:&nbsp; &nbsp; for y in lst:&nbsp; &nbsp; &nbsp; &nbsp; if x.startswith(str(y)):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(x)# 99574404682_0.jpg# 99574404682_1.jpg# 99574449752.jpg# 99581722007.gif这将获取所有以 中提供的数字开头的文件lst。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python