欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python中用shutil.move移動文件或目錄的方法實例

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

0、背景

shutil.move可以實現(xiàn)文件或者目錄的移動。

打印:

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、移動目錄

shutil.move(old,new)用來移動:文件夾:

old是一個目錄
new是一個存在的目錄,這時會把old目錄移動到new下面;可以new也可以是一個不存在的目錄,這時會創(chuàng)建這個不存在的目錄,然后把old目錄下面的所有文件移動到創(chuàng)建的目錄里面。

舉例:

import shutil
# 移動目錄
shutil.move("./folder_123","./folder_456")

./folder_123:

-------------------目錄一定要存在,否則報錯;

./folder_456:

-------------------目錄不存在時,創(chuàng)建該目錄,并將./folder_123目錄下的文件移動到./folder_456目錄下;

-------------------目錄存在時,將folder_123文件夾移動到folder_456文件夾內(nèi);

2、移動文件

shutil.move(old,new)用來移動:文件:

old是一個文件路徑
newnew是一個存在的文件夾路徑或是一個存在的文件夾路徑加文件名

注意:

  • new如果是一個不存在的文件夾路徑,則會將原文件移動到new文件夾上一目錄中,且以該文件夾的名字重命名。
  • new如果是一個不存在的文件夾路徑加文件名,則會報錯。

舉例:

import shutil
# 移動文件
shutil.move("./mask/sample.jpg","./folder_456/folder_789")

./mask/sample.jpg:

-------------------路徑一定要存在,否則報錯;

./folder_456/folder_789:

-------------------目錄存在時,將./mask/sample.jpg文件移動到./folder_456/folder_789目錄下;

-------------------目錄不存在時,具體:folder_456存在,folder_789不存在時,將./mask/sample.jpg移動到folder_456文件夾下,并將sample.jpg文件改名為folder_789;

-------------------目錄不存在時,具體:folder_456不存在,folder_789不存在時,報錯!

總結

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

相關文章

  • Python使用socket的UDP協(xié)議實現(xiàn)FTP文件服務功能

    Python使用socket的UDP協(xié)議實現(xiàn)FTP文件服務功能

    這篇文章主要介紹了Python使用socket的UDP協(xié)議實現(xiàn)FTP文件服務,本示例主要是用Python的socket,使用UDP協(xié)議實現(xiàn)一個FTP服務端、FTP客戶端,用來實現(xiàn)文件的傳輸,需要的朋友可以參考下
    2023-10-10
  • Python 高級庫15 個讓新手愛不釋手(推薦)

    Python 高級庫15 個讓新手愛不釋手(推薦)

    對于初學者來說,這是一種簡單易學的編程語言;另一個原因:大量開箱即用的第三方庫,正是 23 萬個由用戶提供的軟件包使得 Python 真正強大和流行,本文給大家分享15 個讓新手愛不釋手Python 高級庫的相關知識,感興趣的朋友一起看看吧
    2021-05-05
  • 基于Python列表解析(列表推導式)

    基于Python列表解析(列表推導式)

    今天小編就為大家分享一篇基于Python列表解析(列表推導式),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • Python使用eel模塊創(chuàng)建GUI應用程序

    Python使用eel模塊創(chuàng)建GUI應用程序

    在Python中,有許多庫和模塊可以用來創(chuàng)建圖形用戶界面(GUI)應用程序,其中一個流行的選擇是使用eel模塊,下面小編就來為大家詳細介紹一下如何使用eel模塊創(chuàng)建GUI應用程序吧
    2023-12-12
  • Python中Numpy的深拷貝和淺拷貝

    Python中Numpy的深拷貝和淺拷貝

    這篇文章主要介紹了Python中Numpy的深拷貝和淺拷貝,通過講解Python中對Numpy數(shù)組操作的淺拷貝和深拷貝的概念和背后的原理展開全文,需要的小伙伴可以參考一下
    2022-05-05
  • 解決TensorFlow訓練內(nèi)存不斷增長,進程被殺死問題

    解決TensorFlow訓練內(nèi)存不斷增長,進程被殺死問題

    今天小編就為大家分享一篇解決TensorFlow訓練內(nèi)存不斷增長,進程被殺死問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Spring @Enable模塊驅動原理及使用實例

    Spring @Enable模塊驅動原理及使用實例

    這篇文章主要介紹了Spring @Enable模塊驅動原理及使用實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • python編程scrapy簡單代碼實現(xiàn)搜狗圖片下載器

    python編程scrapy簡單代碼實現(xiàn)搜狗圖片下載器

    這篇文章主要為大家介紹了使用python scrapy簡單代碼實現(xiàn)搜狗圖片下載器示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • Python實現(xiàn)讀取文本文件并轉換為pdf

    Python實現(xiàn)讀取文本文件并轉換為pdf

    這篇文章主要為大家詳細介紹了如何使用Python簡便快捷地完成TXT文件到PDF文檔的轉換,滿足多樣化的文檔處理需求,感興趣的小伙伴可以參考下
    2024-04-04
  • 原生python實現(xiàn)knn分類算法

    原生python實現(xiàn)knn分類算法

    這篇文章主要介紹了原生python實現(xiàn)knn分類算法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10

最新評論