为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

桃花长相依 2021-09-11 16:10:37
我有一个包含 600 多个数字的列表,以及包含 50,000 多个文件的目录。所有文件的命名如下:99574404682_0.jpg99574404682_1.jpg99574437307_0.gif99574437307_1.gif99574437307_2.gif99574449752.jpg99574457597.jpg99581722007.gif我想复制名称与列表中的数字匹配的任何文件,直到下划线,然后复制到新目录。例如,如果我的列表包含:995744046829957444975299581722007然后是文件:99574404682_0.jpg99574404682_1.jpg99574449752.jpg99581722007.gif将被复制到一个新目录。我在使用 bash 3.2 的 Mac 上。我在想像 python 这样的东西是我需要使用的,因为列表对于 grep 或 find 来说太大了,但我不确定。谢谢!
查看完整描述

3 回答

?
慕娘9325324

TA贡献1783条经验 获得超4个赞

在python中使用os模块和shutil模块


import os

import 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)


查看完整回答
反对 回复 2021-09-11
?
喵喵时光机

TA贡献1846条经验 获得超7个赞

您可以使用shutil.copy()将文件从源复制到目标。


from shutil import copy


from os import listdir

from os import makedirs


from os.path import abspath

from os.path import exists

from os.path import splitext


filenames = {'99574404682', '99574449752', '99581722007'}


src_path = # your files

dest_path = # where you want to put them


# make the destination if it doesn't exist

if not exists(dest_path):

    makedirs(dest_path)


# go over each file in src_path

for file in listdir(src_path):


    # If underscore in file

    if "_" in file:

        prefix, *_ = file.split("_")


    # otherwise treat as normal file

    else:

        prefix, _ = splitext(file)


    # only copy if prefix exist in above set

    if prefix in filenames:

        copy(abspath(file), dest_path)

这会导致以下文件dest_path:


99574404682_0.jpg  

99574404682_1.jpg  

99574449752.jpg  

99581722007.gif

我不是真正的 bash 专家,但你可以尝试这样的事情:


#!/bin/bash


declare -a arr=("99574404682" "99574449752" "99581722007")


## Example directories, you can change these

src_path="$PWD/*"

dest_path="$PWD/src"


if [ ! -d "$dest_path" ]; then

    mkdir $dest_path

fi


for f1 in $src_path; do 

    filename=$(basename $f1)

    prefix="${filename%.*}"

    IFS='_' read -r -a array <<< $prefix


    for f2 in "${arr[@]}"; do

        if [ "${array[0]}" == "$f2" ]; then

            cp $f1 $dest_path

        fi

    done

done


查看完整回答
反对 回复 2021-09-11
?
慕沐林林

TA贡献2016条经验 获得超9个赞

您可以遍历两个列表,根据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:

    for y in lst:

        if x.startswith(str(y)):

            print(x)


# 99574404682_0.jpg

# 99574404682_1.jpg

# 99574449752.jpg

# 99581722007.gif

这将获取所有以 中提供的数字开头的文件lst。


查看完整回答
反对 回复 2021-09-11
  • 3 回答
  • 0 关注
  • 178 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号