读取项目中所有代码的前30页和后30页 Python脚本

import os
import chardet

f = open('c.txt','a',encoding='utf-8')

codes = []

def getAllDirRE(path, sp = ""):
    #得到当前目录下所有的文件
    filesList = os.listdir(path)
    #处理每一个文件
    sp += "   "
    for fileName in filesList:
        #判断是否是路径(用绝对路径)
        fileAbsPath = os.path.join(path, fileName)
        if os.path.isdir(fileAbsPath):
            print(sp + "目录:", fileName)
            #递归调用
            getAllDirRE(fileAbsPath, sp)
        else:
            arr = fileAbsPath.split('.')
            if arr[len(arr)-1] == 'cs':
            # if '.cs' in fileAbsPath and not '.csv' in fileAbsPath and not '.csproj' in fileAbsPath:
                fb = open(fileAbsPath,'rb')
                code = chardet.detect(fb.read())
                print(fileAbsPath)
                print(code)
                fc = open(fileAbsPath,'r',encoding=code['encoding'])
                codes.extend(fc.readlines())
                fb.close()
                fc.close()
            print(sp + "普通文件:", fileName)

path='C:\\Users\\ccz41\\Desktop\\IOTCode'
getAllDirRE(path)

lineCount = 41
realCodes = ""
totalPageSize = len(codes)/lineCount+1
print('共',len(codes),'行')
for i in range(0,len(codes)):
    if i / lineCount <30 or i / lineCount >(totalPageSize - 30):
        realCodes+=codes[i]

f.write(realCodes)
f.close
posted @ 2021-11-18 11:44  Weekend  阅读(218)  评论(0)    收藏  举报