Я новичок в Django и Python. Я пытаюсь сравнить два CSV-файла и в результате создать новый файл (насколько оба файла отличаются друг от друга) в моем views.py. Сначала я получаю эти файлы и устанавливаю их в переменную, чтобы пользователь мог выбрать и сравнить любой файл с любым именем. Но я получил эту ошибку.
FileNotFoundError at /compare
[Errno 2] No such file or directory: 'fine_name.csv'
Я ожидаю, что когда пользователь выберет два файла и нажмет кнопку сравнения, он получит новый обновленный CSV-файл update.csv.
Вот мой код
views.py
from django.shortcuts import render
from django.http import HttpResponse
def comp(request):
if request.method == 'POST':
file1 = request.POST.get('file1', '')
file2 = request.POST.get('file2','')
with open(file1, 'r') as t1, open(file2, 'r') as t2:
fileone = t1.readlines()
filetwo = t2.readlines()
with open('update.csv', 'w') as outFile:
for line in filetwo:
if line not in fileone:
outFile.write(line)
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="update.csv"'
return response
return render(request,'compare.html')
compare.html
{% extends 'base.html' %}
{% block title %}Comparision{% endblock %}
{% block content %}
<div class="container">
<div class="row justify-content-md-center">
<div class="col-md-6">
<form method="POST" action="">{% csrf_token %}
<h1 class="mb-3 display-4 text-light">Comparision</h1>
<input type="file" id="file1" name="file1" class="form-control mt-3" required autofocus>
<input type="file" id="file2" name="file2" class="form-control mt-3" required autofocus>
<button class="mt-3 col-md-3 btn btn-lg btn-info btn-block" type="submit">Compare</button>
</form>
</div>
</div>
{% endblock %}