リストに記載されているファイルのみ別ディレクトリへコピーする。
環境
- Windows 10 Enterprise 22H2
- Python 3.12.6
サンプル
.
├── from/
│ ├── foo.xxx
│ ├── bar.xxx
│ ├── baz.xxx
│ └── qux.xxx
├── input/
│ └── list.csv
├── to/
└── copy.py
ディレクトリ from
内にコピー元となるファイル
ディレクトリ input
内にコピー対象を指定する CSV ファイル
がそれぞれ配置されていること。
list.csv
foo.xxx
baz.xxx
qux.xxx
ファイル bar.xxx
以外をディレクトリ to
へコピーする例。
copy.py
import csv
import shutil
from pathlib import Path
with open('input/list.csv', encoding='utf_8') as file:
reader = csv.reader(file)
for row in reader:
if Path('from/' + row[0]).is_file():
shutil.copyfile('from/' + row[0], 'to/' + row[0])
else:
print(row[0])