python shutil.move移動(dòng)文件或目錄方式
背景
shutil.move可以實(shí)現(xiàn)文件或者目錄的移動(dòng)。
打?。?/p>
import shutil help(shutil.move) # 打印如下: ''' move(src, dst, copy_function=<function copy2 at 0x000001D1CE15F8C8>) Recursively move a file or directory to another location. This is similar to the Unix "mv" command. Return the file or directory's destination. If the destination is a directory or a symlink to a directory, the source is moved inside the directory. The destination path must not already exist. If the destination already exists but is not a directory, it may be overwritten depending on os.rename() semantics. If the destination is on our current filesystem, then rename() is used. Otherwise, src is copied to the destination and then removed. Symlinks are recreated under the new name if os.rename() fails because of cross filesystem renames. The optional `copy_function` argument is a callable that will be used to copy the source or it will be delegated to `copytree`. By default, copy2() is used, but any function that supports the same signature (like copy()) can be used. A lot more could be done here... A look at a mv.c shows a lot of the issues this implementation glosses over. '''
查看shutil.move函數(shù):
def move(src, dst, copy_function=copy2): """Recursively move a file or directory to another location. This is similar to the Unix "mv" command. Return the file or directory's destination. If the destination is a directory or a symlink to a directory, the source is moved inside the directory. The destination path must not already exist. If the destination already exists but is not a directory, it may be overwritten depending on os.rename() semantics. If the destination is on our current filesystem, then rename() is used. Otherwise, src is copied to the destination and then removed. Symlinks are recreated under the new name if os.rename() fails because of cross filesystem renames. The optional `copy_function` argument is a callable that will be used to copy the source or it will be delegated to `copytree`. By default, copy2() is used, but any function that supports the same signature (like copy()) can be used. A lot more could be done here... A look at a mv.c shows a lot of the issues this implementation glosses over. """ real_dst = dst if os.path.isdir(dst): if _samefile(src, dst): # We might be on a case insensitive filesystem, # perform the rename anyway. os.rename(src, dst) return real_dst = os.path.join(dst, _basename(src)) if os.path.exists(real_dst): raise Error("Destination path '%s' already exists" % real_dst) try: os.rename(src, real_dst) except OSError: if os.path.islink(src): linkto = os.readlink(src) os.symlink(linkto, real_dst) os.unlink(src) elif os.path.isdir(src): if _destinsrc(src, dst): raise Error("Cannot move a directory '%s' into itself" " '%s'." % (src, dst)) copytree(src, real_dst, copy_function=copy_function, symlinks=True) rmtree(src) else: copy_function(src, real_dst) os.unlink(src) return real_dst
移動(dòng)目錄
shutil.move(old,new)用來移動(dòng):文件夾:
old | 是一個(gè)目錄 |
---|---|
new | 是一個(gè)存在的目錄,這時(shí)會(huì)把old目錄移動(dòng)到new下面;可以new也可以是一個(gè)不存在的目錄,這時(shí)會(huì)創(chuàng)建這個(gè)不存在的目錄,然后把old目錄下面的所有文件移動(dòng)到創(chuàng)建的目錄里面。 |
舉例:
import shutil # 移動(dòng)目錄 shutil.move("./folder_123","./folder_456")
./folder_123:
-------------------目錄一定要存在,否則報(bào)錯(cuò);
./folder_456:
-------------------目錄不存在時(shí),創(chuàng)建該目錄,并將./folder_123目錄下的文件移動(dòng)到./folder_456目錄下;
-------------------目錄存在時(shí),將folder_123文件夾移動(dòng)到folder_456文件夾內(nèi);
移動(dòng)文件
shutil.move(old,new)用來移動(dòng):文件:
old | 是一個(gè)文件路徑 |
---|---|
new | new是一個(gè)存在的文件夾路徑或是一個(gè)存在的文件夾路徑加文件名 |
注意:
- new如果是一個(gè)不存在的文件夾路徑,則會(huì)將原文件移動(dòng)到new文件夾上一目錄中,且以該文件夾的名字重命名。
- new如果是一個(gè)不存在的文件夾路徑加文件名,則會(huì)報(bào)錯(cuò)。
舉例:
import shutil # 移動(dòng)文件 shutil.move("./mask/sample.jpg","./folder_456/folder_789")
./mask/sample.jpg:
-------------------路徑一定要存在,否則報(bào)錯(cuò);
./folder_456/folder_789:
-------------------目錄存在時(shí),將./mask/sample.jpg文件移動(dòng)到./folder_456/folder_789目錄下;
-------------------目錄不存在時(shí),具體:folder_456存在,folder_789不存在時(shí),將./mask/sample.jpg移動(dòng)到folder_456文件夾下,并將sample.jpg文件改名為folder_789;
-------------------目錄不存在時(shí),具體:folder_456不存在,folder_789不存在時(shí),報(bào)錯(cuò)!
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)七段數(shù)碼管和倒計(jì)時(shí)效果
今天小編就為大家分享一篇python實(shí)現(xiàn)七段數(shù)碼管和倒計(jì)時(shí)效果,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11pytorch: tensor類型的構(gòu)建與相互轉(zhuǎn)換實(shí)例
今天小編就為大家分享一篇pytorch: tensor類型的構(gòu)建與相互轉(zhuǎn)換實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07python實(shí)現(xiàn)異步回調(diào)機(jī)制代碼分享
本文介紹了python實(shí)現(xiàn)異步回調(diào)機(jī)制的功能,大家參考使用吧2014-01-01OpenCV學(xué)習(xí)記錄python實(shí)現(xiàn)連通域處理函數(shù)
這篇文章主要為大家介紹了OpenCV學(xué)習(xí)記錄python實(shí)現(xiàn)連通域處理函數(shù)cv2.connectedComponentsWithStats()和cv2.connectedComponents()的使用示例詳解2022-06-06Anaconda配置各版本Pytorch的實(shí)現(xiàn)
本文是整理目前全版本pytorch深度學(xué)習(xí)環(huán)境配置指令,以下指令適用Windows操作系統(tǒng),在Anaconda Prompt中運(yùn)行,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Jupyter Notebook折疊輸出的內(nèi)容實(shí)例
這篇文章主要介紹了Jupyter Notebook折疊輸出的內(nèi)容實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04pandas 轉(zhuǎn)換成行列表進(jìn)行讀取與Nan處理的方法
今天小編就為大家分享一篇pandas 轉(zhuǎn)換成行列表進(jìn)行讀取與Nan處理的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10