对整个目录里面的所有文件进行处理.
实用场景:
1)在整个目录下的文件中,哪些文件中有某个关键字出现
2)备份某个目录下面的文件,比如:按照建立日期或者文件名 按照月份进行归档
为什么使用python?
编写简单,调试和执行快(也许C#,Java开始编译的时间,Python已经执行完了)
import re import os import time import shutil my_dir ="//10.1.xx.xx/TestDate" start = time.time() i = 0 startTime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())[0:10] print(startTime) f304013376020 = open("D:/304013376020.txt", 'a') if os.path.isdir(my_dir): for x in os.listdir(my_dir): print(x) with open(os.path.join(my_dir,x), encoding='utf-8') as file2: for ann in file2.readlines(): if "304013376020" in ann: f304013376020.write(x); f304013376020.write(":"); f304013376020.write(ann); f304013376020.write("\n"); print("304013376020 find") i = i + 1; c = time.time() - start print(my_dir +' 目录处理OK!') print('程序运行耗时:%0.2f秒'%(c)) print('总共处理了 %s 个文件'%(i))
标签:python,代码,f304013376020,write,time,print,my,目录,dir From: https://www.cnblogs.com/wonder223/p/17250187.html