• python抽取文件


    功能描述:将源文件夹下文件名中,指定关键字的文件批量复制到另一文件夹中。

    配置文件:searchList.txt  存放关键字

    参数:源文件路径、目标文件路径

    1. import os
    2. import shutil
    3. '''
    4. 将源文件夹下文件名中含有关键字的文件批量复制到另一文件夹中
    5. '''
    6. # copy函数,参数:源文件路径 目标路径 关键字
    7. def searchCopyFile(src_dir_path, to_dir_path, key):
    8. if not os.path.exists(to_dir_path):
    9. # print("to_dir_path not exist,so create the dir")
    10. os.mkdir(to_dir_path, 1)
    11. if os.path.exists(src_dir_path):
    12. # print("src_dir_path exist")
    13. for file in os.listdir(src_dir_path):
    14. # is file
    15. if os.path.isfile(src_dir_path + '/' + file):
    16. if key in file:
    17. # print('找到包含"' + key + '"字符的文件,绝对路径为----->' + src_dir_path + '/' + file)
    18. # print('复制到----->' + to_dir_path + '/' + file)
    19. shutil.copy(src_dir_path + '/' + file, to_dir_path + '/' + file) # 移动用move函数
    20. return True
    21. #src_dir_path = 'D:/workSpace/python_work/learn/CA収納' # 源文件夹 修改1
    22. #to_dir_path = 'D:/workSpace/python_work/learn/CA収納2' # 存放复制文件的文件夹 修改2
    23. path1 = input("Please enter the source path:")
    24. path2 = input("Please enter the target path:")
    25. print("******************************program start************************************")
    26. src_dir_path = path1.replace('\\', '/')
    27. to_dir_path = path2.replace('\\', '/')
    28. file = open('searchList.txt', 'r', encoding='utf-8')
    29. failFile = open('failList.txt', 'w', encoding='utf-8')
    30. totalNum = 0
    31. succeedNum = 0
    32. failNum = 0
    33. # 按行读入
    34. while True:
    35. # 关键词 去除换行\n
    36. key = file.readline().strip('\n')
    37. totalNum = totalNum + 1
    38. if not key:
    39. break
    40. falg = searchCopyFile(src_dir_path, to_dir_path, key)
    41. if falg == True:
    42. print("succeed " + key)
    43. succeedNum = succeedNum + 1
    44. else:
    45. print("fail " + key)
    46. failFile.writelines("fail " + key)
    47. failNum = failNum + 1
    48. file.close()
    49. failFile.close()
    50. print("******************************program end**************************************")
    51. print("source path :" + src_dir_path)
    52. print("target path :" + to_dir_path)
    53. print('totalNum = ' + str(totalNum) + '\t' + "succeedNum = " + str(succeedNum) + '\t' + "failNum =" + str(failNum))
  • 相关阅读:
    mimkatz获取windows10明文密码
    PreScan快速入门到精通第三十一讲基于PreScan进行鱼眼摄像头传感器仿真
    Offset Explorer(Kafka消息可视化工具)报invalid hex digit ‘{‘错误解决方法
    ELK学习笔记1:简介及安装
    GBase 8c系统表-pg_amproc
    objection 基础案例 一
    行测解题笔记完整版
    翁恺C语言程序设计网课笔记合集
    “ChatGPT们”的淘金时代
    Docker-compose部署SonarQube服务
  • 原文地址:https://blog.csdn.net/qq_51649461/article/details/131527538