網站首頁 編程語言 正文
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
相關推薦
- 2023-04-21 python查找指定依賴包簡介信息實現_python
- 2022-07-01 Armbian5.9.0安裝docker及部署可視化portainer的詳細教程_docker
- 2022-09-30 Python?添加命令行參數步驟_python
- 2022-07-09 C++超詳細講解auto與nullptr的使用_C 語言
- 2022-10-22 Kotlin線程的橋接與切換使用介紹_Android
- 2022-08-05 C#?GDI+實現時鐘表盤_C#教程
- 2023-11-17 python中numpy ndarray 按條件篩選數組,關聯篩選的例子——numpyarray對數
- 2023-01-02 Kotlin?FrameLayout與ViewPager2控件實現滾動廣告欄方法_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支