基于匹配符号复制文件

2024-10-01 17:24:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含数千个文件的目录。但是我有一个包含同一目录的文件名的列表。我想复制列表末尾包含星号*的文件

名单是

2016-05-23-0145-30S.INMME_012*
2016-07-12-0546-26S.INMME_006
2016-07-17-2033-16S.INMME_003
2016-07-19-1307-57S.INMME_003
2016-08-17-1649-12S.INMME_006*
2016-09-03-1151-03S.INMME_003
2016-10-21-1240-02S.INMME_006*
2016-10-21-1310-38S.INMME_006
2016-10-23-0016-39S.INMME_006
2016-10-23-0859-50S.INMME_006

所以我想把2016-05-23-0145-30S.INMME_012* 2016-08-17-1649-12S.INMME_006* 2016-10-21-1240-02S.INMME_006*复制到单独的目录中


Tags: 文件目录列表文件名星号末尾名单inmme
2条回答

这个bash脚本将创建一个目录并将文件复制到其中

#!/usr/bin/env bash

new_dir="temp"
mkdir $new_dir

for l in list; do
    dup=$(sed -n '/*$/p' $l)
    cp $dup $new_dir/
done
import os
import shutil
def copy_file_to_another_location(inputpath, outputpath, filename):
   if not os.path.exists(outputpath):
      os.makedirs(outputpath)
   shutil.copy(str(os.path.join(inputpath, filename)).replace("\n", "").replace("*", ""), str(os.path.join(outputpath, filename)).replace("\n", "").replace("*", ""))
   print("Copying: " + str(os.path.join(inputpath, filename)).replace("\n", ""))

inputpath = r'C:\\Users\\me\\Desktop\\tada\\in'
outputpath = r'C:\\Users\\me\\Desktop\\tada\\out'
file = open("asd.txt", "r", encoding="utf-8")
files_list = file.readlines()
for file in files_list:
   if "*" in str(file):
      copy_file_to_another_location(inputpath, outputpath, file)

注意:此脚本将确保将文件夹结构从输入位置保留到输出位置(例如:inputlocation/some_folder/2016-10-23....将被复制到outputlocation/some_folder/2016-10-23...

相关问题 更多 >

    热门问题