流程:

  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. 1-4-1 Windows应用程序组成及编程步骤
  2. html学习笔记之position
  3. Oracle DB 执行表空间时间点恢复
  4. 用批处理来重启IIS的应用程序池
  5. Ecstore 2.0 报表显示空白
  6. hdoj 1824 Let's go home(2-SAT)
  7. svn检出服务器项目中出现的could not connect to server
  8. vue 调用高德地图
  9. EF5.0默认不支持DB First了?
  10. Codeforces Round #431 (Div. 1)
  11. 结构体struct sockaddr_in, struct sockaddr,struct in_addr
  12. [国嵌攻略][044][初始化Bss段]
  13. notify丢失、虚假唤醒
  14. 自动化运维:使用flask+mysql+highcharts搭建监控平台
  15. Python_小学口算题库生成器
  16. 软件测试实验四----mujava变异测试
  17. MVC人员管理系统
  18. Visual Studio Enterprise 2019序列号
  19. Linux核心调度器之周期性调度器scheduler_tick--Linux进程的管理与调度(十八)
  20. luogu P4448 [AHOI2018初中组]球球的排列

热门文章

  1. Node.js 实现第一个应用以及HTTP模块和URL模块应用
  2. Qt编写气体安全管理系统26-组态设计
  3. laravel composer 使用阿里云镜像
  4. 容器版jenkins使用宿主机的kubectl命令
  5. 【Python学习之七】类和对象
  6. QT笔记-QlineEdit
  7. java lambda怎么表达式判断被调用接口名称和接口中方法
  8. c++ 基础学习(二)—— IO 对象
  9. linux系统状态脚本
  10. python基础 — 参数组合