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

python shutil.move移動(dòng)文件或目錄方式

 更新時(shí)間:2024年12月30日 09:49:51   作者:jn10010537  
`shutil.move()`函數(shù)可以移動(dòng)文件或目錄,移動(dòng)目錄時(shí),如果目標(biāo)目錄不存在,會(huì)創(chuàng)建該目錄并將源目錄內(nèi)容移動(dòng)到新目錄;如果目標(biāo)目錄存在,則將源目錄移動(dòng)到目標(biāo)目錄下,移動(dòng)文件時(shí),如果目標(biāo)路徑是目錄,則將文件移動(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)用來(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);

移動(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é)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 如何利用Python動(dòng)態(tài)展示排序算法

    如何利用Python動(dòng)態(tài)展示排序算法

    Python是一種簡(jiǎn)單易學(xué),功能強(qiáng)大的編程語(yǔ)言,它有高效率的高層數(shù)據(jù)結(jié)構(gòu),能夠簡(jiǎn)單、有效地實(shí)現(xiàn)面向?qū)ο缶幊?下面這篇文章主要給大家介紹了關(guān)于如何利用Python動(dòng)態(tài)展示排序算法的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • 使用Numpy讀取CSV文件,并進(jìn)行行列刪除的操作方法

    使用Numpy讀取CSV文件,并進(jìn)行行列刪除的操作方法

    今天小編就為大家分享一篇使用Numpy讀取CSV文件,并進(jìn)行行列刪除的操作方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • Python中如何調(diào)用系統(tǒng)命令和進(jìn)程

    Python中如何調(diào)用系統(tǒng)命令和進(jìn)程

    在Python編程中,subprocess庫(kù)是一個(gè)功能強(qiáng)大的工具,本文將詳細(xì)介紹subprocess庫(kù)的功能和應(yīng)用場(chǎng)景,并通過(guò)代碼示例進(jìn)行說(shuō)明,需要的可以了解下
    2025-02-02
  • Python2和Python3中print的用法示例總結(jié)

    Python2和Python3中print的用法示例總結(jié)

    在Python 3中接觸的第一個(gè)很大的差異就是縮進(jìn)是作為語(yǔ)法的一部分,這和C++等其他語(yǔ)言確實(shí)很不一樣,所以要小心,其中python3和python2中print的用法有很多不同,這篇文章主要給大家介紹了關(guān)于Python2和Python3中print用法的相關(guān)資料,需要的朋友可以參考下。
    2017-10-10
  • Python的多態(tài)性實(shí)例分析

    Python的多態(tài)性實(shí)例分析

    這篇文章主要介紹了Python的多態(tài)性,以實(shí)例形式深入淺出的分析了Python在面向?qū)ο缶幊讨卸鄳B(tài)性的原理與實(shí)現(xiàn)方法,需要的朋友可以參考下
    2015-07-07
  • Pandas之?dāng)?shù)據(jù)追加df.append方式

    Pandas之?dāng)?shù)據(jù)追加df.append方式

    這篇文章主要介紹了Pandas之?dāng)?shù)據(jù)追加df.append方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Python使用Spire.XLS?for?Python實(shí)現(xiàn)TXT轉(zhuǎn)Excel

    Python使用Spire.XLS?for?Python實(shí)現(xiàn)TXT轉(zhuǎn)Excel

    在數(shù)據(jù)處理工作中,我們可能會(huì)遇到將TXT文本文件轉(zhuǎn)換為Excel格式的需求,本文將介紹如何使用?Spire.XLS?for?Python?庫(kù),編寫一個(gè)能夠自動(dòng)檢測(cè)分隔符并完成轉(zhuǎn)換的智能工具,希望對(duì)大家有所幫助
    2026-05-05
  • python應(yīng)用文件讀取與登錄注冊(cè)功能

    python應(yīng)用文件讀取與登錄注冊(cè)功能

    這篇文章主要介紹了python應(yīng)用文件讀取寫登錄注冊(cè)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • selenium+python設(shè)置爬蟲(chóng)代理IP的方法

    selenium+python設(shè)置爬蟲(chóng)代理IP的方法

    這篇文章主要介紹了selenium+python設(shè)置爬蟲(chóng)代理IP的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • 詳解用python自制微信機(jī)器人,定時(shí)發(fā)送天氣預(yù)報(bào)

    詳解用python自制微信機(jī)器人,定時(shí)發(fā)送天氣預(yù)報(bào)

    這篇文章主要介紹了用python自制微信機(jī)器人,定時(shí)發(fā)送天氣預(yù)報(bào),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03

最新評(píng)論

自治县| 宣威市| 嘉义市| 大邑县| 台中县| 崇信县| 绥化市| 彩票| 老河口市| 嘉禾县| 阿勒泰市| 邵东县| 高青县| 申扎县| 竹北市| 延寿县| 朝阳市| 淳安县| 诏安县| 中卫市| 乡宁县| 弋阳县| 白朗县| 沈阳市| 渝中区| 尤溪县| 汤阴县| 时尚| 公主岭市| 崇左市| 台江县| 舒城县| 青阳县| 崇礼县| 乌审旗| 迁安市| 水富县| 峨山| 长白| 定安县| 颍上县|