renames源碼:
def renames(old, new):
head, tail = path.split(new) # 作用是分割為兩部分,head為路徑,tail為文件名;
if head and tail and not path.exists(head): # path.exists(head)檢查路徑是否存在;
makedirs(head)
rename(old, new)
head, tail = path.split(old)
if head and tail:
try:
removedirs(head)
except OSError:
pass
相同點:
1,重命名時,新路徑如果存在,均能在新路徑重命名成功,且刪除原路徑文件;
2,重命名路徑下該文件存在時,均報錯;
區別:
1,重命名時,新路徑如果不存在,os.renames()能新建該路徑后重命名文件;而os.rename()則直接報錯(報錯原因:系統找不到指定的文件);