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 | 是一个文件路径 |
---|---|
new | new是一个存在的文件夹路径或是一个存在的文件夹路径加文件名 |
注意:
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
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 之前在使用Pandas处理csv文件时,发现如果文件名为中文,则会报错:OSError: Initializing from file fa
- 1、异常简介从软件方面来说,错误是语法或是逻辑上的,当python检测到一个错误时,解释器就会指出当前流已经无法继续执行下去,这时候就出现了
- 有时候我们可能需要import另一个路径下的python文件,例如下面这个目录结构,我们想要在_train.py里import在networ
- 在多个文件或者不同语言协同的项目中,python脚本经常需要从命令行直接读取参数。万能的python就自带了argprase包使得这一工作变
- 本文实例讲述了Python数据报表之Excel操作模块用法。分享给大家供大家参考,具体如下:一 点睛Excel是当今最流行的电子表格处理软件
- QTimer控件介绍如果在应用程序中周期性地进行某项操作,比如周期性的检测主机的cpu值,则需要用到QTimer定时器,QTimer类提供了
- 本文实例为大家分享了pygame实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下为了简化起见,游戏素材暂定为两张简单的图片(文中用的是30
- 绘制动图FuncAnimation,它的使用要求简洁且定制化程度较高。如果想将很多图片合并为一个动图,那么ArtistAnimation是最
- 这是官方对SubFolders的介绍:返回由指定文件夹中所有子文件夹(包括隐藏文件夹和系统文件夹)组成的 Folders 集合。object
- 本文实例为大家了Python中列表(List)的详解操作方法,供大家参考,具体内容如下1.创建列表。只要把逗号分隔的不同的数据项使用方括号括
- pytorch在Horovod上训练步骤分为以下几步:import torchimport horovod.torch as hvd# In
- heapq 模块提供了堆算法。heapq是一种子节点和父节点排序的树形数据结构。这个模块提供heap[k] <= heap[2*k+1
- requests模块:python中原生的一款基于网络请求的模块,功能非常强大,简单便捷,效率极高。作用:模拟浏览器发请求。提示:老版使用
- 当前登入的用户以及他(她)的权限可以通过 RequestContext 在模板的context中使用。注意从技术上来说,只有当你使用了 Re
- 1、工厂模式在面向对象编程中,工厂模式是开发过程中最常用的设计模式之一,属于创建型模式,就是通过一个接口函数或对象来创建其他对象并返回,该模
- 调用时输入参数如: www.jb51.net/表示删除www.jb51.net首页的缓存, www.jb51.net/test.
- 前言这篇文章主要介绍了Python 字符串去除空格的6种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,来
- 在开发中有需求在详情显示里外键字段内容,并且添加按钮弹窗内容,以及按钮跳转内容。以前并没有做过相似的开发,我们的后台是xadmin,当时正在
- 一、引子Django 分页查询并返回 json ,需要将返回的 queryset 序列化, demo 如下:# coding=UTF-8im
- 目录一、运行效果二、实现方法1. 爬虫部分2.Tkinter部分三、打包四、代码获取今天咱们用Python爬虫和Tkinter界面来做一个翻