[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创代码] Python遍历目录,搜索指定文件,复制文件到指定目录(学习笔记)

  1. #coding=utf-8
  2. import os
  3. import pathlib
  4. import re
  5. import shutil
  6. # import gol
  7. def check_dir(des_path):
  8.     my_dir = pathlib.Path(des_path)
  9.     if my_dir.is_dir():
  10.         return 0
  11.     else:
  12.         print('{}目录不存在\n正在创建文件夹')
  13.         os.mkdir(des_path)
  14. #搜索文件目录
  15. test_path = r'd:\gin\c'
  16. #复制到指定目录
  17. des_path = r'd:\gin\test_16'
  18. #存储文件
  19. _clist = []
  20. temp = []
  21. if check_dir(des_path) == 0: print('[{}]目录已经存在'.format(des_path))
  22. else: check_dir(des_path)
  23. for root,dirs,files in os.walk(test_path):
  24.     # print('当前目录为:',root)
  25.     # print('当前目录下的子目录为:',dirs)
  26.     for each_file in files:
  27.         #将以.c结尾的文件添加到列表
  28.         temp=re.findall('.*\.c,os.path.join(root,each_file))
  29.         if temp != []: _clist.append(temp)
  30. # os.chdir(des_path)
  31. for each_file in _clist:
  32.     print('copy :',*each_file,'--->',des_path)
  33.     #复制文件
  34.     shutil.copy(*each_file,des_path)
  35.    
  36. print('Done!')
复制代码
1

评分人数

回复 2# 小渣飞


    看不懂,我还没有学习多线程!

TOP

回复 3# netdzb


    这几天都在学习。哈哈哈。。。

TOP

回复 6# netdzb


    C++不会的,会C,C没有对象,学Python我都还木有弄清楚对象!

TOP

返回列表