• 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))
  • 相关阅读:
    学习open62541 --- [69] Client监测多个变量值
    react使用hook封装一个search+input+checkbox组件
    公众号内容拓展学习笔记(2022.7.5)
    uboot移植之环境变量bootcmd
    Java中Spring-ssm整合的理解
    ElasticSearch--分片的路由原理
    cpu设计和实现(数据预取)
    论文阅读/写作扫盲
    Android 13.0 framework层系统手势增加上滑手势home事件功能(相当于Home键)
    IO多路复用
  • 原文地址:https://blog.csdn.net/qq_51649461/article/details/131527538