我有一個包含數千個檔案的目錄。然而我有一個包含同一目錄的檔案名的串列。我想從串列中復制最后含有星號*的檔案。
串列是
2016-05-23014530S.INMME_012*
2016-07-12-0546-26S.INMME_006。
2016-07-17-2033-16S.INMME_003。
2016-07-19-130757S.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*到獨立目錄。
uj5u.com熱心網友回復:
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("
", ").replace("*", "), str(os. path.join(outputpath, filename)).replace("
", "").replace("*", ")
print("復制。" str(os.path.join(inputpath, filename)) .replace("
", " "))
inputpath = r'C:UsersmeDesktop ada/in')
outputpath = r'C:UsersmeDesktop adaout'。
file = open("asd.txt"/span>, "r"/span>, encoding="utf-8"/span>)
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...)
uj5u.com熱心網友回復:
這個bash腳本將創建一個目錄并復制檔案到其中
#!/usr/bin/env bash
new_dir="temp"/span>
mkdir $new_dir
for l in list; do
dup=$(sed -n '/*$/p' $l)
cp $dup $new_dir/
done
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/307701.html
標籤:
