第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何獲取字符串列表并查找名稱與列表中的字符串匹配的文件?

如何獲取字符串列表并查找名稱與列表中的字符串匹配的文件?

桃花長相依 2021-09-11 16:10:37
我有一個包含 600 多個數(shù)字的列表,以及包含 50,000 多個文件的目錄。所有文件的命名如下:99574404682_0.jpg99574404682_1.jpg99574437307_0.gif99574437307_1.gif99574437307_2.gif99574449752.jpg99574457597.jpg99581722007.gif我想復(fù)制名稱與列表中的數(shù)字匹配的任何文件,直到下劃線,然后復(fù)制到新目錄。例如,如果我的列表包含:995744046829957444975299581722007然后是文件:99574404682_0.jpg99574404682_1.jpg99574449752.jpg99581722007.gif將被復(fù)制到一個新目錄。我在使用 bash 3.2 的 Mac 上。我在想像 python 這樣的東西是我需要使用的,因為列表對于 grep 或 find 來說太大了,但我不確定。謝謝!
查看完整描述

3 回答

?
慕娘9325324

TA貢獻(xiàn)1783條經(jīng)驗 獲得超4個贊

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


import os

import shutil

你可以準(zhǔn)備一個包含匹配模式喜歡的列表


match_pattern=['99574404682','99574449752','99581722007']

然后使用 os.listdir() 獲取包含源目錄中文件名的列表


files_in_source_dir=os.listdir(source_directory_path)

最后復(fù)制匹配的文件


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)


查看完整回答
反對 回復(fù) 2021-09-11
?
喵喵時光機(jī)

TA貢獻(xiàn)1846條經(jīng)驗 獲得超7個贊

您可以使用shutil.copy()將文件從源復(fù)制到目標(biāo)。


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)

這會導(dǎo)致以下文件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


查看完整回答
反對 回復(fù) 2021-09-11
?
慕沐林林

TA貢獻(xiàn)2016條經(jīng)驗 獲得超9個贊

您可以遍歷兩個列表,根據(jù)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

這將獲取所有以 中提供的數(shù)字開頭的文件lst。


查看完整回答
反對 回復(fù) 2021-09-11
  • 3 回答
  • 0 關(guān)注
  • 196 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號