流程:

  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)

Flask统计代码行数的更多相关文章

  1. Eclipse统计代码行数

    开发过程中,经常需要统计代码行数,这时可以通过Eclipse的Search功能来实现. 步骤: 1.在Package Explorer中选中需要统计的包: 2.单击菜单Search-->File ...

  2. 在Flash Builder或者Eclipse统计代码行数的方法

    在Flash  Builder或者Eclipse统计代码行数的方法如下图菜单栏--搜索--搜索文件

  3. 【Linux】常用命令-统计代码行数

    公司人员流动大,经常有新的维护任务,交接时喜欢看看新来的模块的代码量,那么问题来了, 如何统计代码行数? 1,最先想到的肯定是 wc. wc -l *.h 将查看[当前目录]下头文件的代码行数,输出结 ...

  4. c#统计代码行数

    小编,已经快学了两年编程了.昨天突发奇想,想统计下这些年到底写过多少行代码,于是做了一个这个小程序来统计代码行数.老规矩,先上图. 比较惭愧,写了两年只有2万多行.那我们还是进入下一项吧. 界面搭建我 ...

  5. 【未解决】对于使用Windows的IDEA进行编译的文件,但无法在Linux系统中统计代码行数的疑问

    在我学习使用Windows的IDEA的过程中,将代码文件转移到Linux虚拟机当中,但无法在Linux系统中统计代码行数. 注意:拷贝进虚拟机的文件均能编译运行. 具体过程如下: root@yogil ...

  6. Visual Studio 统计代码行数

    介绍一种简单的统计代码行数的小技巧, 使用正则表达式,用VS强大的查找功能 b[^:b#/]+.$ 最后结果:

  7. 用django统计代码行数+注释行数

    实现统计代码行数: 1.首先在url.py中配置 from django.conf.urls import url from django.contrib import admin from app0 ...

  8. cloc 统计代码行数工具

    cloc 统计代码行数工具 官网地址:http://cloc.sourceforge.net/ 下载完成后 会生成一个.exe文件 需要修改文件名为 cloc.exe 然后把这个文件拷贝到需要统计的根 ...

  9. python统计代码行数

    以前写了一个java的统计代码行数的小程序,最近在看python,于是就参考前辈的代码,写了一个统计文件夹下面各种程序的代码的小程序,这里贴出来供大家参考 参考链接: https://gist.git ...

随机推荐

  1. Spring cloud微服务安全实战-7-6自定义metrics监控指标(1)

    自己写代码来定义一个metrics,然后让prmetheus收走,在grafana里面定义一个panel并展示出来. prometheus的四种metrics指标.虽然所有的metrics都是数字,但 ...

  2. 【Git】.git/FETCH_HEAD: Permission denied 的解决方法

    背景: 用webhook去拉取代码.报错 .git/FETCH_HEAD: Permission denied 原因分析:.git/FETCH_HEAD的这个文件所属组和所属主是root权限,而我用w ...

  3. 持久化机器学习模型(joblib方式)

    import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression ...

  4. java打包小记

    1.一个类的 Hello.java class Hello{ public static void main(String[] agrs){ System.out.println("hell ...

  5. svn 版本控制搭建

    1.安装SVN yum install subversion 2.开启服务 systemctl start svnserve.service 3.创建仓库 svnadmin create /opt/s ...

  6. LeetCode 503. 下一个更大元素 II(Next Greater Element II)

    503. 下一个更大元素 II 503. Next Greater Element II 题目描述 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 ...

  7. LeetCode 162. 寻找峰值(Find Peak Element) 29

    162. 寻找峰值 162. Find Peak Element 题目描述 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元 ...

  8. [转帖]Windows 7寿终正寝 为何Windows 10屡被吐槽它却无比经典?

    Windows 7寿终正寝 为何Windows 10屡被吐槽它却无比经典? https://www.cnbeta.com/articles/tech/908897.htm 是的,一代经典操作系统Win ...

  9. 对于并发任务,应该使用 Task 替代 BackgroundWorker

    背景 EF + Oracle,并发存储监控记录,使用 BackgroundWorker 时产生错误如下: public void MonitorLogging(DateTime DateStart, ...

  10. hyper-v安装windows7

    win7镜像下载地址 http://msdn.itellyou.cn/ 该网站都是微软系列的正规软件 非常好用 在hyper-v 虚拟机安装windows系统时,到百度搜索了几个iso 都不好用 到h ...