在 Python 中剪切和粘贴文件或目录 [重复]

Posted

技术标签:

【中文标题】在 Python 中剪切和粘贴文件或目录 [重复]【英文标题】:Cut and Paste a File or Directory in Python [duplicate] 【发布时间】:2014-12-14 03:35:50 【问题描述】:

我需要将大量数据移动到一个驱动器上的不同位置,因此剪切和粘贴会快得多。目前,我只是在使用 shutil.copytree 和 shutil.rmtree,虽然可以,但速度很慢。

有没有办法剪切/粘贴文件而不是复制/删除?

【问题讨论】:

***.com/questions/8858008/moving-a-file-in-python 【参考方案1】:

shutil.move()

>>> import shutil
>>> shutil.move(source, destination)

os.rename()

>>> import os
>>> os.rename(source, destination)

【讨论】:

您可能需要编辑您的答案以进行一次更正:您已将 sourcedestination 交换,所以它应该是 shutil.move(source, destination) @Pupil 谢谢,已更新

以上是关于在 Python 中剪切和粘贴文件或目录 [重复]的主要内容,如果未能解决你的问题,请参考以下文章