最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python中用shutil.move移動(dòng)文件或目錄的方法實(shí)例

 更新時(shí)間:2022年12月24日 11:09:44   作者:jn10010537  
在python操作中大家對(duì)os,shutil,sys,等通用庫(kù)一定不陌生,下面這篇文章主要給大家介紹了關(guān)于python中用shutil.move移動(dòng)文件或目錄的相關(guān)資料,需要的朋友可以參考下

0、背景

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

1、移動(dòng)目錄

shutil.move(old,new)用來(lái)移動(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);

2、移動(dòng)文件

shutil.move(old,new)用來(lái)移動(dòng):文件:

old是一個(gè)文件路徑
newnew是一個(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é)

到此這篇關(guān)于python中用shutil.move移動(dòng)文件或目錄的文章就介紹到這了,更多相關(guān)python shutil.move移動(dòng)文件目錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python實(shí)現(xiàn)決策樹(shù)C4.5算法的示例

    Python實(shí)現(xiàn)決策樹(shù)C4.5算法的示例

    本篇文章主要介紹了Python實(shí)現(xiàn)決策樹(shù)C4.5算法的示例,詳解的介紹了決策樹(shù)C4.5算法的原理和實(shí)現(xiàn)代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2018-05-05
  • Django?RestFramework?全局異常處理詳解

    Django?RestFramework?全局異常處理詳解

    這篇文章主要為大家詳細(xì)介紹了Django?RestFramework?全局異常處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-02-02
  • 面向新手解析python Beautiful Soup基本用法

    面向新手解析python Beautiful Soup基本用法

    這篇文章主要介紹了面向新手解析python Beautiful Soup基本用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • python鏈表的基礎(chǔ)概念和基礎(chǔ)用法詳解

    python鏈表的基礎(chǔ)概念和基礎(chǔ)用法詳解

    這篇文章主要為大家詳細(xì)介紹了python鏈表的基礎(chǔ)概念和基礎(chǔ)用法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • python中查看變量?jī)?nèi)存地址的方法

    python中查看變量?jī)?nèi)存地址的方法

    這篇文章主要介紹了python中查看變量?jī)?nèi)存地址的方法,涉及Python中id使用技巧,需要的朋友可以參考下
    2015-05-05
  • Python爬蟲(chóng)實(shí)戰(zhàn)之虎牙視頻爬取附源碼

    Python爬蟲(chóng)實(shí)戰(zhàn)之虎牙視頻爬取附源碼

    讀萬(wàn)卷書(shū)不如行萬(wàn)里路,學(xué)的扎不扎實(shí)要通過(guò)實(shí)戰(zhàn)才能看出來(lái),本篇文章手把手帶你爬取虎牙短視頻數(shù)據(jù),大家可以在實(shí)戰(zhàn)過(guò)程中查缺補(bǔ)漏,加深學(xué)習(xí)
    2021-10-10
  • 最新評(píng)論

    永春县| 临澧县| 昌乐县| 洪洞县| 肃宁县| 锡林浩特市| 永福县| 潞城市| 化德县| 定结县| 晋中市| 即墨市| 麟游县| 无为县| 永兴县| 万盛区| 侯马市| 上饶市| 毕节市| 甘谷县| 赤峰市| 南昌县| 莱芜市| 民县| 红安县| 临江市| 平湖市| 九台市| 海安县| 武隆县| 古丈县| 延庆县| 康平县| 钟祥市| 吉安市| 乾安县| 惠东县| 铜鼓县| 唐山市| 友谊县| 抚顺市|