当前位置: 首页 > news >正文

python移动文件

测试1(直接把B文件夹移动到了A里,成为了A的子文件夹)

import os
import shutil# 移动文件夹,B文件夹在当前目录没有了,跑到了A的子文件里
##
shutil.move('./example1/B/', './example1/A/')

在这里插入图片描述

测试2(B文件不动,将B文件里的所有的子文件夹移动到A内)

import os
import shutildef move_folder(source_folder, destination_folder,overlapp=False):# 创建目标文件夹os.makedirs(destination_folder, exist_ok=True)destFileFolder = os.listdir(destination_folder)# 遍历源文件夹中的所有文件和文件夹for subFileFolder in os.listdir(source_folder):print(subFileFolder)source_item = os.path.join(source_folder, subFileFolder)if(subFileFolder in destFileFolder):if(not overlapp):print("原始文件夹{}不移动!!!".format(source_item))else:# 把原始的文件给删除,然后再移动print("未实现,应该首先把这个结果给删除点,然后复制过来。。。。")else:shutil.move(source_item, destination_folder)print("{}-->移动-->{}完成!!!".format(source_item,destination_folder))
# 示例用法# delete_list
file_path ='./example2/A/'
file_list = os.listdir(file_path)
# 从文件夹中删掉
if(".DS_Store" in file_list):print("There is .DS_Store in filefolder, delete it!!!")file_list.remove(".DS_Store")os.remove(file_path+"/.DS_Store")
else:print("No .DS_Store in file folder")move_folder("./example2/B/","./example2/A/")

结果如下
在这里插入图片描述

移动子文件夹

import pickle 
import os 
import shutil
read_path="/Users/yxk/Desktop/核磁项目/527检查修改mask/ThreeD_patient_list.pkl"
base_sample_path = "/Volumes/My Passport/yxk_move_sample/file/"
with open(read_path,"rb") as f:  # Python 3: open(..., 'rb')ThreeD_patient_list=pickle.load(f)print(ThreeD_patient_list)for patient in ThreeD_patient_list:shutil.move(base_sample_path+patient,"/Volumes/My Passport/yxk_move_sample/ThreeDT1样本/")
http://www.lryc.cn/news/359011.html

相关文章:

  • eNSP学习——OSPF的DR与BDR
  • 【文献阅读】应用人工智能在Simulink中开发软件
  • 【计算机毕设】基于SpringBoot的房产销售系统设计与实现 - 源码免费(私信领取)
  • Docker 私有仓库部署和管理
  • 大模型时代的具身智能系列专题(六)
  • Pytorch入门需要达到的效果
  • 数据结构的快速排序(c语言版)
  • 数据结构基础篇(4)
  • 使用cad绘制一个螺旋输送机
  • 迭代器模式(行为型)
  • Django——Admin站点(Python)
  • React 组件通信
  • 【再探】设计模式—访问者模式、策略模式及状态模式
  • 新人硬件工程师,工作中遇到的问题list
  • 如何在Linux系统中搭建Zookeeper集群
  • C++:vector的模拟实现
  • QT系列教程(5) 模态对话框消息传递
  • Linux学习笔记(清晰且清爽)
  • 2.5Bump Mapping 凹凸映射
  • 数字化前沿:Web3如何引领未来技术演进
  • 【kubernetes】探索k8s集群的存储卷、pvc和pv
  • UI线程和工作线程
  • RandLA-Net 训练自定义数据集
  • 洛谷 B3642:二叉树的遍历 ← 结构体方法 链式前向星方法
  • 飞腾+FPGA多U多串全国产工控主机
  • uni-app实现页面通信EventChannel
  • 等保系列之——网络安全等级保护测评工作流程及工作内容
  • 自然语言处理中的BERT模型深度剖析
  • 数据结构:希尔排序
  • unicloud 云对象