python中用shutil.move移动文件或目录的方法实例

作者:jn10010537 时间:2021-01-03 07:35:06 

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

来源:https://blog.csdn.net/jn10010537/article/details/121596611

标签:shutil.move,移动文件,目录
0
投稿

猜你喜欢

  • python组合无重复三位数的实例

    2021-11-18 03:59:13
  • 使用Numpy读取CSV文件,并进行行列删除的操作方法

    2023-05-05 03:26:11
  • Python创建模块及模块导入的方法

    2023-04-21 03:42:03
  • ajax请求get与post的区别总结

    2024-04-29 13:58:25
  • PHP图片上传代码

    2024-05-05 09:17:26
  • innerHTML,outerHTML,innerText,outerText用法

    2008-02-15 12:22:00
  • 深入浅析python 协程与go协程的区别

    2022-02-16 23:57:26
  • asp中文件与文件夹常用处理函数(文件后缀、创建文件等)

    2011-02-20 11:00:00
  • python中web框架的自定义创建

    2023-09-18 14:54:24
  • django中使用原生sql语句的方法步骤

    2023-06-29 16:36:07
  • PHP与Web页面交互操作实例分析

    2023-09-05 14:43:16
  • 一文教你快速生成MySQL数据库关系图

    2024-01-26 15:06:52
  • 浅谈开启magic_quote_gpc后的sql注入攻击与防范

    2024-01-13 01:12:11
  • 在Python中执行cmd

    2022-05-20 07:24:50
  • 不同版本中Python matplotlib.pyplot.draw()界面绘制异常问题的解决

    2021-04-23 03:41:16
  • Java动态-代理实现AOP

    2023-07-15 09:33:43
  • vue项目中安装less依赖的过程

    2024-05-10 14:21:12
  • 用 Javascript 验证表单(form)中多选框(checkbox)值

    2024-04-10 10:39:14
  • PyCharm 2020 激活到 2100 年的教程

    2021-01-19 12:30:43
  • MySQL事务管理的作用详解

    2024-01-23 21:20:37
  • asp之家 网络编程 m.aspxhome.com