Python は shutil を使用して、ファイルのコピー、ファイルの移動、フォルダーの再帰的削除、名前の変更、圧縮と解凍を行います。

shutil は、ファイルやディレクトリのコピー、削除、移動などの操作を処理するための Python 標準ライブラリのモジュールです。よく使用されるshutil関数をいくつか示します。

コピー コピー

copy(src, dest):srcソース ファイルをターゲット ファイルにコピーしますdest

import shutil

shutil.copy("d:/python3_test/one.txt", "d:/python3_test/two.txt")

copytree(src, dest):srcソース ディレクトリとそのサブディレクトリおよびファイルをターゲット ディレクトリにコピーしますdest

ファイルを移動する

move(src, dest):ソース ファイルまたはディレクトリをターゲット ファイルまたはディレクトリにsrc移動または名前変更しますdest

import shutil

shutil.move("d:/python3_test/one.txt", "d:/python3_test/three.txt")

フォルダを再帰的に削除する

rmtree(path): 指定されたディレクトリpathとそのサブディレクトリおよびファイルを再帰的に削除します。

重命名

rename(old, new):ファイルまたはディレクトリの名前を にold変更しますnew

圧縮

import shutil

shutil.make_archive("archive_name", 'zip', root_dir="D:\python3_test", base_dir="shutil_test")

リンク

link(src, dest):srcソース ファイルまたはディレクトリから宛先ファイルまたはディレクトリ dest へのハード リンクを作成します。
symlink(src, dest):srcソース ファイルまたはディレクトリからターゲット ファイルまたはディレクトリへのシンボリック リンクを作成しますdest

参考

https://docs.python.org/zh-cn/3/library/shutil.html

おすすめ

転載: blog.csdn.net/lilongsy/article/details/132068964