Comments (4)
经实测后总结,上述列出的三个内置函数在移动或重命名文件时主要针对 目标路径已存在 和 目标中间路径不存在 这两种特殊情况的处理方式不同。
目标路径已存在:
os.rename
: 引发 FileExistsErroros.renames
: 引发 FileExistsErrorshutil.move
: 当目标路径为文件时,若源路径也为文件则覆盖,若源路径是目录则引发 shutil.Error;当目标路径为目录时,若目标路径下不存在与源路径重名的文件或目录,则以目标路径作为父目录,否则引发 shutil.Error
目标中间路径不存在:
os.rename
: 引发 FileNotFoundErroros.renames
: 创建,包括任何中间目录shutil.move
: 引发 FileNotFoundError
from systempath.
os.renames
和 shutil.move
的源码注释。
os.renames
os.rename
的Super版,它是对 os.rename
的增强。在移动或重命名文件时,若目标路径不存在则会创建目标路径(包括任何中间目录);在移动或重命名文件后,若源路径为空则会删除源路径(从后往前删,直到整个路径被使用或找到一个非空目录)。
shutil.move
递归地将文件或目录移动到另一个位置,类似于Unix的 mv
命令,返回值为目标路径。如果目标是目录或指向目录的符号链接,则将源移动到目录内,目标路径必须不存在。如果目标已经存在,但不是一个目录,它可能会根据 os.rename
语义被覆盖。
如果目标位于当前文件系统上,则使用 os.rename
。否则,源路径将被复制到目标路径,然后删除。如果 os.rename
因为跨文件系统重命名而失败,则符号链接将在目标路径下重新创建。
可选的 copy_function
参数是一个可调用对象,它将被用于复制源代码,或者被委托给copytree
。默认情况下,使用 copy2
,但可以使用支持相同签名的任何函数(如copy
)。
from systempath.
综合分析,我们决定使用 os.rename
作为重命名方法,它是三者中最基础的,轻量级的,运行速度最快。使用 shutil.move
作为移动方法,它类似于Unix系统的 mv
命令,功能更全面,且更容易被大众所接受。另,前者亦可移动,后者亦可重命名。
from systempath.
在经过长时间的“回味”,以及更多的思考和**斗争后,我们放弃原方案。原方案中存在我们片面的理解,也许并不符合开发者的需求,也许开发者有更深透的理解。现方案是,将尽量多的方法都提供出来,供开发者自行抉择(找到最贴近需求的那一个并用之):
def rename(self, dst: PathLink, /) -> PathLink:
# Rename the file, call `os.rename` internally.
def renames(self, dst: PathLink, /) -> PathLink:
# Rename the file, super version of `self.rename`. Call `os.renames`
# internally.
def replace(self, dst: PathLink, /) -> PathLink:
# Rename the file, overwrite if destination exists. Call `os.replace`
# internally.
def move(self, dst: PathLink, /, ...) -> PathLink:
# Move the file, call `shutil.move` internally.
from systempath.
Related Issues (8)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from systempath.