ValueError : I/O operation on closed file











up vote
59
down vote

favorite
13












import csv    

with open('v.csv', 'w') as csvfile:
cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)

for w, c in p.iteritems():
cwriter.writerow(w + c)


Here, p is a dictionary, w and c both are strings.



When I try to write in the file it reports error:



ValueError : I/O operation on closed file.


Help me, I'm really new to python. I'm working with Python 2.7.3
Thank you in advance.










share|improve this question




























    up vote
    59
    down vote

    favorite
    13












    import csv    

    with open('v.csv', 'w') as csvfile:
    cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)

    for w, c in p.iteritems():
    cwriter.writerow(w + c)


    Here, p is a dictionary, w and c both are strings.



    When I try to write in the file it reports error:



    ValueError : I/O operation on closed file.


    Help me, I'm really new to python. I'm working with Python 2.7.3
    Thank you in advance.










    share|improve this question


























      up vote
      59
      down vote

      favorite
      13









      up vote
      59
      down vote

      favorite
      13






      13





      import csv    

      with open('v.csv', 'w') as csvfile:
      cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)

      for w, c in p.iteritems():
      cwriter.writerow(w + c)


      Here, p is a dictionary, w and c both are strings.



      When I try to write in the file it reports error:



      ValueError : I/O operation on closed file.


      Help me, I'm really new to python. I'm working with Python 2.7.3
      Thank you in advance.










      share|improve this question















      import csv    

      with open('v.csv', 'w') as csvfile:
      cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)

      for w, c in p.iteritems():
      cwriter.writerow(w + c)


      Here, p is a dictionary, w and c both are strings.



      When I try to write in the file it reports error:



      ValueError : I/O operation on closed file.


      Help me, I'm really new to python. I'm working with Python 2.7.3
      Thank you in advance.







      python csv file-io io






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 23 '17 at 12:17









      Ciro Santilli 新疆改造中心 六四事件 法轮功

      130k27509439




      130k27509439










      asked Sep 23 '13 at 6:08









      GobSmack

      68731227




      68731227
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          94
          down vote



          accepted










          Indent correctly; for statement should be inside with block:



          import csv    

          with open('v.csv', 'w') as csvfile:
          cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)

          for w, c in p.iteritems():
          cwriter.writerow(w + c)


          Outside the with block, the file is closed.



          >>> with open('/tmp/1', 'w') as f:
          ... print f.closed
          ...
          False
          >>> print f.closed
          True





          share|improve this answer





















          • Thanks! You saved me hours of waste efforts :)
            – Learner23
            Jul 14 '15 at 5:38










          • Can't believe that this error was looked up so many times!!
            – user1767754
            Dec 17 '17 at 22:59










          • @user1767754, I faced this error: after doing a copy/paste of a code coming from Stackoverflow.com. Spaces & Tabs were mixed
            – Slake
            Mar 26 at 21:31


















          up vote
          2
          down vote













          Same error can raise by mixing: tabs + spaces.



          with open('/foo', 'w') as f:
          (spaces OR tab) print f <-- success
          (spaces AND tab) print f <-- fail





          share|improve this answer





















          • True, but this is always the case in python when mixing them up right?
            – Nebulosar
            Apr 20 at 9:24











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18952716%2fvalueerror-i-o-operation-on-closed-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          94
          down vote



          accepted










          Indent correctly; for statement should be inside with block:



          import csv    

          with open('v.csv', 'w') as csvfile:
          cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)

          for w, c in p.iteritems():
          cwriter.writerow(w + c)


          Outside the with block, the file is closed.



          >>> with open('/tmp/1', 'w') as f:
          ... print f.closed
          ...
          False
          >>> print f.closed
          True





          share|improve this answer





















          • Thanks! You saved me hours of waste efforts :)
            – Learner23
            Jul 14 '15 at 5:38










          • Can't believe that this error was looked up so many times!!
            – user1767754
            Dec 17 '17 at 22:59










          • @user1767754, I faced this error: after doing a copy/paste of a code coming from Stackoverflow.com. Spaces & Tabs were mixed
            – Slake
            Mar 26 at 21:31















          up vote
          94
          down vote



          accepted










          Indent correctly; for statement should be inside with block:



          import csv    

          with open('v.csv', 'w') as csvfile:
          cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)

          for w, c in p.iteritems():
          cwriter.writerow(w + c)


          Outside the with block, the file is closed.



          >>> with open('/tmp/1', 'w') as f:
          ... print f.closed
          ...
          False
          >>> print f.closed
          True





          share|improve this answer





















          • Thanks! You saved me hours of waste efforts :)
            – Learner23
            Jul 14 '15 at 5:38










          • Can't believe that this error was looked up so many times!!
            – user1767754
            Dec 17 '17 at 22:59










          • @user1767754, I faced this error: after doing a copy/paste of a code coming from Stackoverflow.com. Spaces & Tabs were mixed
            – Slake
            Mar 26 at 21:31













          up vote
          94
          down vote



          accepted







          up vote
          94
          down vote



          accepted






          Indent correctly; for statement should be inside with block:



          import csv    

          with open('v.csv', 'w') as csvfile:
          cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)

          for w, c in p.iteritems():
          cwriter.writerow(w + c)


          Outside the with block, the file is closed.



          >>> with open('/tmp/1', 'w') as f:
          ... print f.closed
          ...
          False
          >>> print f.closed
          True





          share|improve this answer












          Indent correctly; for statement should be inside with block:



          import csv    

          with open('v.csv', 'w') as csvfile:
          cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)

          for w, c in p.iteritems():
          cwriter.writerow(w + c)


          Outside the with block, the file is closed.



          >>> with open('/tmp/1', 'w') as f:
          ... print f.closed
          ...
          False
          >>> print f.closed
          True






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 23 '13 at 6:09









          falsetru

          240k31414416




          240k31414416












          • Thanks! You saved me hours of waste efforts :)
            – Learner23
            Jul 14 '15 at 5:38










          • Can't believe that this error was looked up so many times!!
            – user1767754
            Dec 17 '17 at 22:59










          • @user1767754, I faced this error: after doing a copy/paste of a code coming from Stackoverflow.com. Spaces & Tabs were mixed
            – Slake
            Mar 26 at 21:31


















          • Thanks! You saved me hours of waste efforts :)
            – Learner23
            Jul 14 '15 at 5:38










          • Can't believe that this error was looked up so many times!!
            – user1767754
            Dec 17 '17 at 22:59










          • @user1767754, I faced this error: after doing a copy/paste of a code coming from Stackoverflow.com. Spaces & Tabs were mixed
            – Slake
            Mar 26 at 21:31
















          Thanks! You saved me hours of waste efforts :)
          – Learner23
          Jul 14 '15 at 5:38




          Thanks! You saved me hours of waste efforts :)
          – Learner23
          Jul 14 '15 at 5:38












          Can't believe that this error was looked up so many times!!
          – user1767754
          Dec 17 '17 at 22:59




          Can't believe that this error was looked up so many times!!
          – user1767754
          Dec 17 '17 at 22:59












          @user1767754, I faced this error: after doing a copy/paste of a code coming from Stackoverflow.com. Spaces & Tabs were mixed
          – Slake
          Mar 26 at 21:31




          @user1767754, I faced this error: after doing a copy/paste of a code coming from Stackoverflow.com. Spaces & Tabs were mixed
          – Slake
          Mar 26 at 21:31












          up vote
          2
          down vote













          Same error can raise by mixing: tabs + spaces.



          with open('/foo', 'w') as f:
          (spaces OR tab) print f <-- success
          (spaces AND tab) print f <-- fail





          share|improve this answer





















          • True, but this is always the case in python when mixing them up right?
            – Nebulosar
            Apr 20 at 9:24















          up vote
          2
          down vote













          Same error can raise by mixing: tabs + spaces.



          with open('/foo', 'w') as f:
          (spaces OR tab) print f <-- success
          (spaces AND tab) print f <-- fail





          share|improve this answer





















          • True, but this is always the case in python when mixing them up right?
            – Nebulosar
            Apr 20 at 9:24













          up vote
          2
          down vote










          up vote
          2
          down vote









          Same error can raise by mixing: tabs + spaces.



          with open('/foo', 'w') as f:
          (spaces OR tab) print f <-- success
          (spaces AND tab) print f <-- fail





          share|improve this answer












          Same error can raise by mixing: tabs + spaces.



          with open('/foo', 'w') as f:
          (spaces OR tab) print f <-- success
          (spaces AND tab) print f <-- fail






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 26 at 21:43









          Slake

          1,29621724




          1,29621724












          • True, but this is always the case in python when mixing them up right?
            – Nebulosar
            Apr 20 at 9:24


















          • True, but this is always the case in python when mixing them up right?
            – Nebulosar
            Apr 20 at 9:24
















          True, but this is always the case in python when mixing them up right?
          – Nebulosar
          Apr 20 at 9:24




          True, but this is always the case in python when mixing them up right?
          – Nebulosar
          Apr 20 at 9:24


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18952716%2fvalueerror-i-o-operation-on-closed-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini