首頁 > 軟體

python中用shutil.move移動檔案或目錄的方法範例

2022-12-25 14:01:33

0、背景

shutil.move可以實現檔案或者目錄的移動。

列印:

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函數:

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也可以是一個不存在的目錄,這時會建立這個不存在的目錄,然後把old目錄下面的所有檔案移動到建立的目錄裡面。

舉例:

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

./folder_123:

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

./folder_456:

-------------------目錄不存在時,建立該目錄,並將./folder_123目錄下的檔案移動到./folder_456目錄下;

-------------------目錄存在時,將folder_123資料夾移動到folder_456資料夾內;

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移動檔案目錄內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com