shutil.moveの使い方
shutil.move 関数は、ファイルやディレクトリを別の場所に移動するための Python の標準ライブラリ shutil に含まれる関数です。以下に基本的な使い方を示します。
python
import shutil import os # 移動元のファイルパス source_path = '/path/to/source/file.txt' # 移動先のディレクトリパス destination_directory = '/path/to/destination/' # 移動先にファイルを移動する shutil.move(source_path, destination_directory)
この例では、source_path で指定したファイルを destination_directory で指定したディレクトリに移動しています。移動先のディレクトリが存在しない場合は自動的に作成されます。
shutil.move はファイルだけでなく、ディレクトリも移動できます。ただし、ディレクトリを別のディレクトリに移動する場合、移動先のディレクトリは存在している必要があります。
python
import shutil import os # 移動元のディレクトリパス source_directory = '/path/to/source_directory/' # 移動先の親ディレクトリパス destination_parent_directory = '/path/to/destination_parent/' # 移動先のディレクトリ名 destination_directory_name = 'destination_directory' # 移動先にディレクトリを移動する shutil.move(source_directory, os.path.join(destination_parent_directory, destination_directory_name))
この例では、source_directory で指定したディレクトリを destination_parent_directory で指定したディレクトリの中に destination_directory_name として移動しています。

ディスカッション
コメント一覧
まだ、コメントがありません