-1

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

列表是

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*到单独的目录。

4

2 回答 2

1
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...

于 2021-09-14T14:54:50.537 回答
0

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
于 2021-09-14T15:25:33.793 回答