Python 文件和文件夹的操作 shutil 模块

Python shutil模块

shutil模块操作 移动、改名和删除文件和文件夹
复制文件
import shutil
import os
optSrcPath = "C:\\Users\\Administrator\\Desktop\\opt"#测试文件目录
# optDesPath = "C:\\Users\\Administrator\\Desktop\\opt\\test"#测试目标目录
optDesPath = os.path.join(optSrcPath,"test")#测试目标目录
fileName = "test.txt"#测试目标文件
shutil.copy(os.path.join(optSrcPath,fileName),os.path.join(optDesPath,fileName))#复制文件  目标路径必须存在  如果目标目录有同名的文件会覆盖
复制文件夹
import shutil
optSrcPath = "C:\\Users\\Administrator\\Desktop\\opt\\test"#测试目标目录
optDesPath = "C:\\Users\\Administrator\\Desktop\\test"#测试文件目录
#  The destination directory must not already exist.
newPath = shutil.copytree(optSrcPath,optDesPath)#返回
移动文件
import shutil
import os
optSrcPath = "C:\\Users\\Administrator\\Desktop\\opt"#测试文件目录
# optDesPath = "C:\\Users\\Administrator\\Desktop\\opt\\test"#测试目标目录
optDesPath = os.path.join(optSrcPath,"test")#测试目标目录
fileName = "test.txt"#测试目标文件
shutil.move(os.path.join(optSrcPath,fileName),optDesPath)#复制文件  目标路径必须存在 并且不能存在同名文件
移动文件夹
import shutil
optSrcPath = "C:\\Users\\Administrator\\Desktop\\opt\\test"#测试目标目录
optDesPath = "C:\\Users\\Administrator\\Desktop\\test"#测试文件目录

newPath = shutil.move(optSrcPath,optDesPath)#返回
删除文件夹
#小心操作不可逆
import shutil
optSrcPath = "C:\\Users\\Administrator\\Desktop\\opt\\test"#测试目标目录
optDesPath = "C:\\Users\\Administrator\\Desktop\\test"#测试文件目录
shutil.rmtree(optDesPath)#返回
shutil.rmtree(optSrcPath)#被删除文件夹必须存在
删除文件或文件夹可用 send2trash 可在回收站还原
import send2trash
import os
optPath = "C:\\Users\\Administrator\\Desktop\\opt"#测试目标目录
fileName = "a.txt"
send2trash.send2trash(os.path.join(optPath,fileName))
send2trash.send2trash(optPath)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`shutil` 模块Python 中用于对文件文件夹进行操作的标准库之一。它提供了一些简单而又强大的函数,使得文件文件夹的复制、移动、删除等操作变得非常容易。 下面是一些常见的 `shutil` 模块中的函数及其用法: 1. `shutil.copy(src, dst)`:将文件从源路径复制到目标路径。如果目标路径是一个文件,则覆盖该文件;如果目标路径是一个目录,则在该目录下创建同名文件并复制。 ```python import shutil src_path = "source_file.txt" dst_path = "destination_file.txt" shutil.copy(src_path, dst_path) ``` 2. `shutil.copy2(src, dst)`:与 `shutil.copy()` 类似,但还会复制文件的元数据(如创建时间、修改时间等)。 ```python import shutil src_path = "source_file.txt" dst_path = "destination_file.txt" shutil.copy2(src_path, dst_path) ``` 3. `shutil.move(src, dst)`:将文件或目录从源路径移动到目标路径。如果目标路径是一个文件,则覆盖该文件;如果目标路径是一个目录,则在该目录下创建同名文件或目录并移动。 ```python import shutil src_path = "source_file.txt" dst_path = "destination_file.txt" shutil.move(src_path, dst_path) ``` 4. `shutil.rmtree(path)`:递归删除一个目录及其内容。 ```python import shutil dir_path = "directory_to_delete" shutil.rmtree(dir_path) ``` 5. `shutil.make_archive(base_name, format, root_dir)`:创建一个压缩包,并返回压缩包的文件路径。`base_name` 是压缩包的文件名,`format` 是压缩包的格式(如 "zip"、"tar" 等),`root_dir` 是要打包的根目录。 ```python import shutil dir_to_archive = "directory_to_archive" archive_name = "archive" archive_format = "zip" shutil.make_archive(archive_name, archive_format, dir_to_archive) ``` 以上是 `shutil` 模块中常用的一些函数,更多函数可以查看 Python 官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值