0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Python でリスト情報に基づいてファイルをコピーする

Posted at

リストに記載されているファイルのみ別ディレクトリへコピーする。

環境

  • 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])
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?