当前位置: 代码网 > it编程>前端脚本>Python > python shutil.move移动文件或目录方式

python shutil.move移动文件或目录方式

2024年12月30日 Python 我要评论
背景shutil.move可以实现文件或者目录的移动。打印:import shutilhelp(shutil.move)# 打印如下:'''move(src, dst, copy_function=&

背景

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

移动目录

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文件夹内;

移动文件

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不存在时,报错!

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com