|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
想实现统计文件夹内总共的代码行数。实现将代码名称与行数这一字典保存成文件时发现没法判断某一字符串是否在文本文件里,应该如何解决呢?
- # -*- coding: utf-8 -*-
- """
- Created on Tue Jul 23 10:01:46 2019
- @author: u
- """
- import glob
- import datetime
- import re
- nowTime=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')#现在
- file_name1 = r'Total_code_count.txt'
- file_name2 = r'Code_count_details.txt'
- files = glob.glob(r'D:\FishC_learning\*.py')
- len_dict = {}
- total_count = 0
- for each_py in files:
- this_count = 0
- with open(each_py,'r', encoding='UTF-8') as py:
- for each_line in py:
- this_count += 1
- len_dict[each_py] = this_count
- total_count += this_count
-
- #print(len_dict.items())
- print(total_count)
- with open(file_name1,'a+') as f1:
- strings =nowTime + '时刻本文件夹的代码总数为:' + str(total_count) + '\n'
- print(strings)
- f1.writelines(strings)
- with open(file_name2,'r+') as f2:
- for each_py in len_dict.keys():
- if each_py not in f2.read():
- strings2 = str(each_py) + ' 的代码数为:' + str(len_dict[each_py]) + '\n'
- print(strings2)
- f2.writelines(strings2)
复制代码 |
|