如果是单数的话只需要把
date1% 2 == 0 换成 date1% 2 != 0 即可
import os path='e:/DSX/LX/day6/logs/android' for root, dirs, files in os.walk(path): # 遍历路径下的所有目录及文件 for file in files: filename = file.split('.')[0] # 文件名用.分隔 date = filename.split('-')[2] # 文件名再次分隔取到日期 date1=int(date)#字符串转换成整型 if date1% 2 == 0:# with open(os.path.join(path,file), 'a+', encoding='utf-8') as f: f.write('今天太阳好漂亮')
老师写的代码
import os path='e:DSXLXday6logs' for abs_path,dir,file in os.walk(path): for f in file: day=f.split('.')[0].split('-')[-1] if int(day)%2==0: filename=os.path.join(abs_path,f) open(filename,'a+',encoding='utf-8').write('写写写,脖子疼')