I am confused with my Python. ValueError: I/O operation on closed file












-4















Kida lost here. I am trying to get a consolidated csv and I keep getting this error:




File "consolidate.py", line 26, in csv_merge.write(line)



ValueError: I/O operation on closed file.




I tried moving indentation:



csv_header='name,location,age,phonenumber'
csv_out = 'consolidated.csv'

csv_d
dir_tree = os.walk(csv_dir)
for dirpath, dirnames, filenames in dir_tree:
pass

csv_list =
for file in filenames:
if file.endswith('.csv'):
csv_list.append(file)

csv_merge = open(csv_out, 'w')
csv_merge.write(csv_header)
csv_merge.write('n')ir = os.getcwd()
for file in csv_list:
csv_in = open(file)
for line in csv_in:
if line.startswith(csv_header):
continue
csv_merge.write(line)
csv_in.close()
csv_merge.close()
print('Verify consolidated CSV file : ' + csv_out)


But this didn't work. How can I resolve this error?










share|improve this question




















  • 6





    You never open() csv_merge

    – G. Anderson
    Nov 14 '18 at 22:36











  • csv_merge != csv_in

    – JacobIRR
    Nov 14 '18 at 22:37











  • How was csv_merge initialized?

    – DYZ
    Nov 14 '18 at 22:37






  • 2





    Even if you did open csv_merge, you close it each time through the loop.

    – kindall
    Nov 14 '18 at 22:39











  • I have edited my question.

    – MrKarma4u
    Nov 14 '18 at 22:52
















-4















Kida lost here. I am trying to get a consolidated csv and I keep getting this error:




File "consolidate.py", line 26, in csv_merge.write(line)



ValueError: I/O operation on closed file.




I tried moving indentation:



csv_header='name,location,age,phonenumber'
csv_out = 'consolidated.csv'

csv_d
dir_tree = os.walk(csv_dir)
for dirpath, dirnames, filenames in dir_tree:
pass

csv_list =
for file in filenames:
if file.endswith('.csv'):
csv_list.append(file)

csv_merge = open(csv_out, 'w')
csv_merge.write(csv_header)
csv_merge.write('n')ir = os.getcwd()
for file in csv_list:
csv_in = open(file)
for line in csv_in:
if line.startswith(csv_header):
continue
csv_merge.write(line)
csv_in.close()
csv_merge.close()
print('Verify consolidated CSV file : ' + csv_out)


But this didn't work. How can I resolve this error?










share|improve this question




















  • 6





    You never open() csv_merge

    – G. Anderson
    Nov 14 '18 at 22:36











  • csv_merge != csv_in

    – JacobIRR
    Nov 14 '18 at 22:37











  • How was csv_merge initialized?

    – DYZ
    Nov 14 '18 at 22:37






  • 2





    Even if you did open csv_merge, you close it each time through the loop.

    – kindall
    Nov 14 '18 at 22:39











  • I have edited my question.

    – MrKarma4u
    Nov 14 '18 at 22:52














-4












-4








-4








Kida lost here. I am trying to get a consolidated csv and I keep getting this error:




File "consolidate.py", line 26, in csv_merge.write(line)



ValueError: I/O operation on closed file.




I tried moving indentation:



csv_header='name,location,age,phonenumber'
csv_out = 'consolidated.csv'

csv_d
dir_tree = os.walk(csv_dir)
for dirpath, dirnames, filenames in dir_tree:
pass

csv_list =
for file in filenames:
if file.endswith('.csv'):
csv_list.append(file)

csv_merge = open(csv_out, 'w')
csv_merge.write(csv_header)
csv_merge.write('n')ir = os.getcwd()
for file in csv_list:
csv_in = open(file)
for line in csv_in:
if line.startswith(csv_header):
continue
csv_merge.write(line)
csv_in.close()
csv_merge.close()
print('Verify consolidated CSV file : ' + csv_out)


But this didn't work. How can I resolve this error?










share|improve this question
















Kida lost here. I am trying to get a consolidated csv and I keep getting this error:




File "consolidate.py", line 26, in csv_merge.write(line)



ValueError: I/O operation on closed file.




I tried moving indentation:



csv_header='name,location,age,phonenumber'
csv_out = 'consolidated.csv'

csv_d
dir_tree = os.walk(csv_dir)
for dirpath, dirnames, filenames in dir_tree:
pass

csv_list =
for file in filenames:
if file.endswith('.csv'):
csv_list.append(file)

csv_merge = open(csv_out, 'w')
csv_merge.write(csv_header)
csv_merge.write('n')ir = os.getcwd()
for file in csv_list:
csv_in = open(file)
for line in csv_in:
if line.startswith(csv_header):
continue
csv_merge.write(line)
csv_in.close()
csv_merge.close()
print('Verify consolidated CSV file : ' + csv_out)


But this didn't work. How can I resolve this error?







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 2:19









Joel

1,5686719




1,5686719










asked Nov 14 '18 at 22:34









MrKarma4uMrKarma4u

12




12








  • 6





    You never open() csv_merge

    – G. Anderson
    Nov 14 '18 at 22:36











  • csv_merge != csv_in

    – JacobIRR
    Nov 14 '18 at 22:37











  • How was csv_merge initialized?

    – DYZ
    Nov 14 '18 at 22:37






  • 2





    Even if you did open csv_merge, you close it each time through the loop.

    – kindall
    Nov 14 '18 at 22:39











  • I have edited my question.

    – MrKarma4u
    Nov 14 '18 at 22:52














  • 6





    You never open() csv_merge

    – G. Anderson
    Nov 14 '18 at 22:36











  • csv_merge != csv_in

    – JacobIRR
    Nov 14 '18 at 22:37











  • How was csv_merge initialized?

    – DYZ
    Nov 14 '18 at 22:37






  • 2





    Even if you did open csv_merge, you close it each time through the loop.

    – kindall
    Nov 14 '18 at 22:39











  • I have edited my question.

    – MrKarma4u
    Nov 14 '18 at 22:52








6




6





You never open() csv_merge

– G. Anderson
Nov 14 '18 at 22:36





You never open() csv_merge

– G. Anderson
Nov 14 '18 at 22:36













csv_merge != csv_in

– JacobIRR
Nov 14 '18 at 22:37





csv_merge != csv_in

– JacobIRR
Nov 14 '18 at 22:37













How was csv_merge initialized?

– DYZ
Nov 14 '18 at 22:37





How was csv_merge initialized?

– DYZ
Nov 14 '18 at 22:37




2




2





Even if you did open csv_merge, you close it each time through the loop.

– kindall
Nov 14 '18 at 22:39





Even if you did open csv_merge, you close it each time through the loop.

– kindall
Nov 14 '18 at 22:39













I have edited my question.

– MrKarma4u
Nov 14 '18 at 22:52





I have edited my question.

– MrKarma4u
Nov 14 '18 at 22:52












1 Answer
1






active

oldest

votes


















0














You never open csv_merge properly and even if you do you still close csv_merge after the first item in csv_list is written.



Why are you even using things like csv_merge.close()?

The convention is to use things like with open(csv_merge, 'w') as f:, that way the file always gets closed, even if the loop or script fails to execute properly.






share|improve this answer























    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',
    autoActivateHeartbeat: false,
    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%2f53309752%2fi-am-confused-with-my-python-valueerror-i-o-operation-on-closed-file%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You never open csv_merge properly and even if you do you still close csv_merge after the first item in csv_list is written.



    Why are you even using things like csv_merge.close()?

    The convention is to use things like with open(csv_merge, 'w') as f:, that way the file always gets closed, even if the loop or script fails to execute properly.






    share|improve this answer




























      0














      You never open csv_merge properly and even if you do you still close csv_merge after the first item in csv_list is written.



      Why are you even using things like csv_merge.close()?

      The convention is to use things like with open(csv_merge, 'w') as f:, that way the file always gets closed, even if the loop or script fails to execute properly.






      share|improve this answer


























        0












        0








        0







        You never open csv_merge properly and even if you do you still close csv_merge after the first item in csv_list is written.



        Why are you even using things like csv_merge.close()?

        The convention is to use things like with open(csv_merge, 'w') as f:, that way the file always gets closed, even if the loop or script fails to execute properly.






        share|improve this answer













        You never open csv_merge properly and even if you do you still close csv_merge after the first item in csv_list is written.



        Why are you even using things like csv_merge.close()?

        The convention is to use things like with open(csv_merge, 'w') as f:, that way the file always gets closed, even if the loop or script fails to execute properly.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 23:05









        jtwilsonjtwilson

        325




        325
































            draft saved

            draft discarded




















































            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 other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309752%2fi-am-confused-with-my-python-valueerror-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







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma