python判断是文件还是目录的注意事项
1个回答
展开全部
# -*- coding: utf-8 -*-
import os,re
def statCodeLines(path,file):
if re.match(r'.*py$',file):
lines_blank = 0
lines_comment = 0
lines_total = 0
f = open(path+'\\'+file,'r')
lines = f.readlines()
lines_total = len(lines)
for line in lines:
pattern_blank = re.compile(r'\s*$')
pattern_comment = re.compile(r'\s*#')
if pattern_blank.match(line):
lines_blank += 1
if pattern_comment.match(line):
lines_comment += 1
f.close()
print(lines_total,lines_comment,lines_blank)
return True
else:
pass
def getFiles(path):
filelist = os.listdir(path)
for file in filelist:
fpath = path + '\\' + file
# 做判断时需要传入完整文件路径
if(os.path.isfile(fpath)):
statCodeLines(path,file)
if(os.path.isdir(fpath)):
getFiles(fpath)
if __name__ == '__main__':
#statCodeLines('D:\\Documents\\Visual Studio 2015\\Projects\\PracticeRecord\\PracticeRecord','Random_string.py')
getFiles('D:\\Documents\\Visual Studio 2015\\Projects\\PracticeRecord\\PracticeRecord')
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询