У меня ниже представление django
from io import TextIOWrapper
def get_header_data(file_obj):
# perform some operations like
text_file = TextIOWrapper(file_obj)
reader = csv.reader(text_file)
.....
.....
file_obj.seek(0)
print(file_obj.closed, "--> Inside get_header_data method")
return some_data
def upload(request):
file_obj = request.FILES["file"]
print(file_obj.closed, "--> Inside upload before entering in to get_header_data method")
# Get some headers
header_data = get_header_data(file_obj)
# Facing an error at this point file_obj.seek(0)
print(file_obj.closed, "--> Inside upload after returned from get_header_data method")
file_obj.seek(0)
Вывод :
False--> Inside upload before entering in to get_header_data method
False--> Inside get_header_data method
True--> Inside upload after returned from get_header_data method
ValueError: I/O operation on closed file at line 5 (file_obj.seek(0)) inside upload method
Проблема здесь, в случае python3, file_obj
, котораяЯ послал в get_header_data
из upload
метод закрывается python, когда интерпретатор выходит из функции загрузки (когда он возвращается).
Тот же самый код работает нормально в Python 2.7
, так чтоможет быть проблема здесь и почему file_obj
закрывается, когда переводчик вышел из get_header_data
метода