6月16日 Django作業 文件解壓縮統計行數
阿新 • • 發佈:2018-06-20
HERE In real 練習 ima dal cut com 表達式
作業要求:
前端頁面註意:
自己寫的:
from django.shortcuts import render, HttpResponse import zipfile import re # Create your views here. def op_zip(zip_file): zfile = zipfile.ZipFile(zip_file, ‘r‘) zfile_name_list = zfile.namelist() # zfile_name_list = list(filter(lambda x: re.findall(‘\.[a-z]+$‘, x), zfile_name_list))# print(zfile_name_list) # html_files = filter(lambda x: x.endswith(‘.html‘), zfile_name_list) # py_files = filter(lambda x: x.endswith(‘.py‘), zfile_name_list) # css_files = filter(lambda x: x.endswith(‘.css‘), zfile_name_list) # js_files = filter(lambda x: x.endswith(‘.js‘), zfile_name_list)count_file = {‘html‘: 0, ‘py‘: 0, ‘css‘: 0, ‘js‘: 0} for file_name in zfile.namelist(): # if file_name.endswith(‘.zip‘): # child_zip_file = zfile.getinfo(file_name) # print(zipfile.is_zipfile(file_name)) # # data = zfile.read(file_name)# print(data) # res = op_zip(child_zip_file) # for k in res: # count_file[k] += res[k] if file_name.endswith(‘.html‘): data = zfile.read(file_name) data_list = data.decode(‘utf8‘).split(‘\r\n‘) zhu = 0 html_count = 0 for line in data_list: if line.startswith(‘//‘): continue if line.startswith(‘<!--‘) or line.startswith(‘/*‘): zhu += 1 if line.endswith(‘-->‘) or line.endswith(‘*/‘): zhu -= 1 if zhu == 0: html_count += 1 count_file[‘html‘] += html_count elif file_name.endswith(‘.py‘): data = zfile.read(file_name) data_list = data.decode(‘utf8‘).split(‘\r\n‘) zhu = 0 py_count = 0 for line in data_list: if line.startswith(‘#‘): continue if line.startswith("\‘\‘\‘") or line.startswith(‘\"\"\"‘): zhu += 1 if line.endswith("\‘\‘\‘") or line.endswith(‘\"\"\"‘): zhu -= 1 if zhu == 0 and line: py_count += 1 count_file[‘py‘] = py_count elif file_name.endswith(‘.css‘): data = zfile.read(file_name) data_list = data.decode(‘utf8‘).split(‘\r\n‘) zhu = 0 css_count = 0 for line in data_list: if line.startswith(‘//‘): continue if line.startswith(‘/*‘): zhu += 1 if line.endswith(‘*/‘): zhu -= 1 if zhu == 0: css_count += 1 count_file[‘css‘] += css_count elif file_name.endswith(‘.js‘): data = zfile.read(file_name) data_list = data.decode(‘utf8‘).split(‘\r\n‘) zhu = 0 js_count = 0 for line in data_list: if line.startswith(‘//‘): continue if line.startswith(‘/*‘): zhu += 1 if line.endswith(‘*/‘): zhu -= 1 if zhu == 0: js_count += 1 count_file[‘js‘] += js_count print(count_file) return count_file def upload(request): if request.method == ‘POST‘: name = request.POST.get(‘name‘) upload_file = request.FILES.get(‘upload_file‘) count_file = op_zip(upload_file) print(count_file) return render(request, ‘result.html‘, {‘name‘: name, ‘result‘: count_file}) return render(request, ‘upload.html‘)
存在的問題:
1、沒有考慮到上傳文件時,上傳的文件類型是否是壓縮包
2、只對zip類型的壓縮包進行解壓
3、忘記了shutil模塊
4、過程中遇到的問題:
正則表達式幾乎都忘記了,前端界面bootstrap用的也磕磕絆絆的 ,還是缺少練習
老師的答案:
from django.shortcuts import render, HttpResponse import shutil import os import uuid from django.conf import settings ACCEPT_FILE_TYPE = ["zip", "tar", "gztar", "rar"] # Create your views here. def upload(request): if request.method == "POST": # 拿到上傳的文件對象 file_obj = request.FILES.get("code_file") # prefix:前綴 suffix:後綴 # 將上傳文件的文件名字 從右邊按 ‘.‘切割(最多切一次) filename, suffix = file_obj.name.rsplit(".", maxsplit=1) # 如果上傳的文件類型不是可接受的,就直接返回錯誤提示 if suffix not in ACCEPT_FILE_TYPE: return HttpResponse("上傳文件必須是壓縮文件") # 上傳文件的類型正確 # 在項目的根目錄下新建一個和上傳文件同名的文件 with open(file_obj.name, "wb") as f: # 從上傳文件對象一點一點讀取數據 for chunk in file_obj.chunks(): # 將數據寫入我新建的文件 f.write(chunk) # 拼接得到上傳文件的全路徑 real_file_path = os.path.join(settings.BASE_DIR, file_obj.name) # 對上傳的文件做處理 upload_path = os.path.join(settings.BASE_DIR, "files", str(uuid.uuid4())) # 解壓代碼文件至指定文件夾 shutil.unpack_archive(real_file_path, extract_dir=upload_path) # 代碼行數統計 total_num = 0 for (dir_path, dir_names, filenames) in os.walk(upload_path): # dir_path: 根目錄 dir_names: 文件夾 filenames: 文件 # 遍歷所有的文件 for filename in filenames: # 將文件名和根目錄拼接成完整的路徑 file_path = os.path.join(dir_path, filename) # 完整的路徑按照‘.‘ 進行右切割 (最大切割一次) file_path_list = file_path.rsplit(".", maxsplit=1) # 文件沒有後綴名直接跳過 if len(file_path_list) != 2: continue # 如果不是py文件直接跳過 if file_path_list[1] != "py": continue # 初始化一個存放當前文件代碼行數的變量 line_num = 0 with open(file_path, "r", encoding="utf8") as f: # 一行一行讀取 for line in f: # 如果是註釋就跳過 if line.strip().startswith("#"): continue # 否則代碼行數+1 line_num += 1 total_num += line_num return render( request, "show.html", {"file_name": file_obj.name, "file_size": file_obj.size, "total_num": total_num, "value": "張曌"}) return render(request, "upload.html")
6月16日 Django作業 文件解壓縮統計行數