Flask统计代码行数
2024-08-22 09:54:50
流程:
1.获取前端的文件
2.判断文件是否zip文件
3.解压压缩包并保存
4.遍历解压后的文件夹
5.判断文件是否py文件,将绝对路径添加到列表
6.循环列表,排除注释和空号,统计行数
from flask import Blueprint
from flask import render_template
from flask import request
from flask import current_app
import time
import os
import shutil uploadBlue = Blueprint("uploadBlue",__name__) @uploadBlue.route("/upload",methods=["GET","POST"])
def upload():
error = ""
if request.method == "POST":
# 获取前端的上传的文件
file = request.files.get("file")
# 判断后缀是否zip的文件
zip_file = file.filename.rsplit(".",1)
if zip_file[-1] != "zip":
return render_template("upload.html", error="上传的不是zip文件,请重新上传") # 解压保存
t = int(time.time())
upload_path = os.path.join(current_app.config.root_path,"files",str(t))
try:
shutil._unpack_zipfile(file,upload_path) # 解压文件
except Exception as e:
print(e)
shutil.rmtree(upload_path) # 删除非空文件夹
return render_template("upload.html", error="文件解压异常") # 遍历保存的文件夹得到所有.py文件
file_list = []
for (dirpath,dirname,filenames) in os.walk(upload_path):
# 判断文件是否py结尾,如果是则将绝对路径添加到列表
for file in filenames:
if file.rsplit(".",1)[-1] == "py":
file_path = os.path.join(upload_path,file)
file_list.append(file_path)
num = 0
for pyfile in file_list:
with open(pyfile,mode='rb') as f:
for line in f:
# 井号开头或者空行则跳过
if line.replace(b" ",b"").strip().startswith(b"#") or not line.strip():
continue
num += 1
return str(num) return render_template("upload.html",error=error)
最新文章
- 1-4-1 Windows应用程序组成及编程步骤
- html学习笔记之position
- Oracle DB 执行表空间时间点恢复
- 用批处理来重启IIS的应用程序池
- Ecstore 2.0 报表显示空白
- hdoj 1824 Let's go home(2-SAT)
- svn检出服务器项目中出现的could not connect to server
- vue 调用高德地图
- EF5.0默认不支持DB First了?
- Codeforces Round #431 (Div. 1)
- 结构体struct sockaddr_in, struct sockaddr,struct in_addr
- [国嵌攻略][044][初始化Bss段]
- notify丢失、虚假唤醒
- 自动化运维:使用flask+mysql+highcharts搭建监控平台
- Python_小学口算题库生成器
- 软件测试实验四----mujava变异测试
- MVC人员管理系统
- Visual Studio Enterprise 2019序列号
- Linux核心调度器之周期性调度器scheduler_tick--Linux进程的管理与调度(十八)
- luogu P4448 [AHOI2018初中组]球球的排列