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

Python 如何获取字符串列表并查找名称与列表中字符串匹配的文件?,python,bash,macos,command-line,Python,Bash,Macos,Command Line,我有一个600多个数字的列表和一个50000多个文件的目录。所有文件的名称如下所示: 99574404682_0.jpg 99574404682_1.jpg 99574437307_0.gif 99574437307_1.gif 99574437307_2.gif 99574449752.jpg 99574457597.jpg 99581722007.gif #!/bin/bash declare -a arr=("99574404682" "99574449752" "9958172200

我有一个600多个数字的列表和一个50000多个文件的目录。所有文件的名称如下所示:

99574404682_0.jpg
99574404682_1.jpg
99574437307_0.gif
99574437307_1.gif
99574437307_2.gif
99574449752.jpg
99574457597.jpg
99581722007.gif
#!/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
我想复制任何名称与列表中的数字匹配的文件,直到下划线,然后复制到新目录

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

99574404682
99574449752
99581722007
然后文件:

99574404682_0.jpg
99574404682_1.jpg
99574449752.jpg
99581722007.gif

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

您可以根据
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
中提供的数字开头的所有文件您可以根据
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

中提供的数字开头的文件,您可以使用这些数字将文件从源复制到目标

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\u路径中生成以下文件:

99574404682_0.jpg  
99574404682_1.jpg  
99574449752.jpg  
99581722007.gif
我不是bash方面的专家,但您可以尝试以下方法:

99574404682_0.jpg
99574404682_1.jpg
99574437307_0.gif
99574437307_1.gif
99574437307_2.gif
99574449752.jpg
99574457597.jpg
99581722007.gif
#!/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
#/bin/bash
声明-a arr=(“99574404682”“99574449752”“9958172007”)
##示例目录,您可以更改这些
src_path=“$PWD/*”
dest_path=“$PWD/src”
如果[!-d“$dest_path”];然后
mkdir$dest_路径
fi
对于$src_路径中的f1;做
文件名=$(基本名称$f1)
前缀=“${filename%.*}”
IFS=''''''read-r-a数组可以用于将文件从源复制到目标

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\u路径中生成以下文件:

99574404682_0.jpg  
99574404682_1.jpg  
99574449752.jpg  
99581722007.gif
我不是bash方面的专家,但您可以尝试以下方法:

99574404682_0.jpg
99574404682_1.jpg
99574437307_0.gif
99574437307_1.gif
99574437307_2.gif
99574449752.jpg
99574457597.jpg
99581722007.gif
#!/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
#/bin/bash
声明-a arr=(“99574404682”“99574449752”“9958172007”)
##示例目录,您可以更改这些
src_path=“$PWD/*”
dest_path=“$PWD/src”
如果[!-d“$dest_path”];然后
mkdir$dest_路径
fi
对于$src_路径中的f1;做
文件名=$(基本名称$f1)
前缀=“${filename%.*}”

在python中使用os模块和shutil模块的IFS='''''read-r-a数组

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)

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

你能发布一些已经尝试过的代码吗?一个快速代码:
,同时读取数字;执行cp/source/directory/$number[.]*/dest/dir2>/dev/null;完成
(假设每行一个数字)。谢谢!今晚我来试试,效果很好。非常感谢。你能发布一些已经尝试过的代码吗?一个快速代码:
,同时读取数字;执行cp/source/directory/$number[.]*/dest/dir2>/dev/null;完成
(假设每行一个数字)。谢谢!今晚我来试试,效果很好。非常感谢。