|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
如图,1.为什么我把print(count1)去掉count1返回的是0,改成return count1 也是返回的0
2,open(i,‘rb‘)打开.py类型的文件不是可以用'r'的形式,为什么这儿要用'rb'不然就会报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xa5 in position 22: illegal multibyte sequence
- from easygui import *
- import os
- def search_file(path):
- os.chdir(path)
- for each_file in os.listdir(os.curdir):
- (file_name,extend) = os.path.splitext(each_file)
- if extend == '.py':
- py_file.append(os.getcwd()+os.sep+each_file)
- if os.path.isdir(each_file):
- search_file(each_file)
- os.chdir(os.pardir)
- py_file = []
- search_path = diropenbox()
- search_file(search_path)
- def count_line(files,count1):
- for i in files:
- f = open(i,'rb')
- for each_line in f:
- count1 += 1
- print(count1)
- count1 = 0
- count_line(py_file,count1)
复制代码
- from easygui import *
- import os
- def search_file(path):
- os.chdir(path)
- for each_file in os.listdir(os.curdir):
- (file_name,extend) = os.path.splitext(each_file)
- if extend == '.py':
- py_file.append(os.getcwd()+os.sep+each_file)
- # 如果这条 if 语句为真,你的程序 100% 会报错!
- if os.path.isdir(each_file):
- search_file(each_file)
- os.chdir(os.pardir)
- py_file = []
- search_path = diropenbox('请选择你的代码库')
- search_file(search_path)
- def count_line(files,count1):
- for i in files:
- f = open(i,encoding='utf-8')
- for each_line in f:
- count1 += 1
- return count1
- count1 = 0
- # 这里需要对 count1 变量进行重新赋值!
- count1 = count_line(py_file,count1)
- file_number = len(py_file)
- lack_row = 100000 - count1
- percentage = str(count1/100000*100)[:4] + '%'
- msg = '您目前共累计编写了%d行代码,完成进度:%s\n离10万行代码还差%d行,请继续努力!' % (count1,percentage,lack_row)
- title = '显示文件内容'
- text = '【.py】源文件%d个,源代码%d行' % (file_number,count1)
- textbox(msg,title,text)
复制代码
如果您对我的答案感到满意,请设置 [b]最佳答案[b]!谢谢
|
|