Paul
Paul

Reputation: 6737

I/O operation on closed file only with large file (>3M)

I use django and save uploaded file with the same code:

destination = open(directory + newfilename, 'wb+')
for chunk in docfile.chunks():
    destination.write(chunk)
    destination.close()

But when I upload large file (~3M and more) I get this error:

ValueError: I/O operation on closed file

On destination.write(chunk) line. How can I fix this?

Upvotes: 2

Views: 469

Answers (1)

Zhongzhi
Zhongzhi

Reputation: 436

I think you closed the file in the for loop.. Try to put close outside.

    destination = open(directory + newfilename, 'wb+')
    for chunk in docfile.chunks():
        destination.write(chunk)
    destination.close()

Upvotes: 4

Related Questions