1.shutil.move(src, dst)
在Python中,可以使用shutil
模块来进行文件和文件夹的迁移操作。
shutil.move(src, dst)
: 将文件从src
路径移动到dst
路径。如果dst
是一个目录,文件将被移动到该目录中。如果dst
是已经存在的文件名,那么会覆盖该文件。
import shutil
shutil.move('path/to/source/file', 'path/to/destination')
注意move后原文件夹中文件将不存在。dst可以是一个目录也可以是一个文件。
1. shutil.copy(src, dst)
复制文件从src
路径到dst
路径。如果dst
是一个目录,文件将被复制到该目录中。如果dst
是已经存在的文件名,那么会覆盖该文件。
import shutil
shutil.copy('path/to/source/file', 'path/to/destination')
注意:如果需要在两个文件系统之间迁移文件(例如,从一个硬盘到另一个硬盘),可能需要使用os.rename()
或os.link()
,或者使用第三方库如pyvirtualfs
。