import csv v open("v.csv", "w") as csvfile: cwriter = csv.writer(csvfile, delimiter=" ", quotechar="|", quoting=csv.QUOTE_MINIMAL)for w, c in p.items(): cwriter.writerow(w + c)Here, p is a dictionary, w and c both space strings.

You are watching: I/o operation on closed file

When I shot to create to the file it reports the error:

ValueError: I/O procedure on closeup of the door file.

*

*

Indent correctly; her for statement must be inside the v block:

import csv with open("v.csv", "w") together csvfile: cwriter = csv.writer(csvfile, delimiter=" ", quotechar="|", quoting=csv.QUOTE_MINIMAL) because that w, c in p.items(): cwriter.writerow(w + c)Outside the through block, the file is closed.

*

*

*

I was acquiring this exemption when debugging in PyCharm, offered that no breakpoint to be being hit. To avoid it, I included a breakpoint simply after the v block, and also then it quit happening.


file = open("filename.txt", newline="")for row in self.data: print(row)Save data come a variable(file), therefore you need a with.


I had this problem when i was using an unknown variable inside the with open(...) as f:.I gotten rid of (or I characterized outside) the unknown variable and the trouble disappeared.


Thanks for contributing an answer to stack Overflow!

Please be sure to answer the question. Provide details and share your research!

But avoid

Asking for help, clarification, or responding to various other answers.Making statements based on opinion; ago them increase with referrals or an individual experience.

See more: Under The Hat Of Scott Mckay ' Patriot Street Fighter Scott Mckay Youtube

To discover more, check out our tips on writing great answers.


write-up Your price Discard

By click “Post your Answer”, friend agree to our regards to service, privacy policy and cookie policy


Not the prize you're feather for? Browse various other questions tagged python csv file-io io or asking your own question.


UnicodeEncodeError: 'ascii' codec can't encode character u'xa0' in position 20: ordinal not in range(128)
site design / logo © 2021 stack Exchange Inc; user contributions license is granted under cc by-sa. Rev2021.10.29.40598


your privacy

By click “Accept every cookies”, you agree stack Exchange have the right to store cookie on your an equipment and disclose information in accordance with our Cookie Policy.