File Iteration Returns No Data in files












-1















My Script below is supposed to iterate through files located in this folder, with the ending csv. It reaches each file, but deletes all the data, instead of turning the 'NA' to 0.



import csv
import os
import sys

#file location
folderpath = (r'\Deturbansci19cvi2$ClientServicesCC&ACurrentSSOBRD WorkParts OpportunitySample data')
#print (folderpath)

for root,dirs,files in os.walk(folderpath, topdown = False):
for name in files:
if 'Archive' not in root and name.endswith ('.csv'):
f = os.path.join(r'\Deturbansci19cvi2$ClientServicesCC&ACurrentSSOBRD WorkParts OpportunitySample data\'+name)
#print (os.path.join(root, name))
newfile = (r'\Deturbansci19cvi2$ClientServicesCC&ACurrentSSOBRD WorkParts OpportunitySample data\'+name)
with open(f, 'rb') as csvfile, open(newfile,'w',newline='') as newcsvfile:
csv_reader = csv.reader(csvfile, delimiter = ',')
csv_writer = csv.writer(newcsvfile, delimiter=',')

for lines in csv_reader:
#Replacing lines
replaced = lines[7].replace('NA', '0')
lines[7] = replaced
replaced = lines[8].replace('NA', '0')
lines[8] = replaced
replaced = lines[17].replace('NA', '0')
lines[17] = replaced
replaced = lines[18].replace('NA', '0')
lines[18] = replaced
replaced = lines[19].replace('NA', '0')
lines[19] = replaced
replaced = lines[20].replace('NA', '0')
lines[20] = replaced
replaced = lines[21].replace('NA', '0')
lines[21] = replaced
replaced = lines[22].replace('NA', '0')
lines[22] = replaced
replaced = lines[23].replace('NA', '0')
lines[23] = replaced
replaced = lines[24].replace('NA', '0')
lines[24] = replaced
replaced = lines[25].replace('NA', '0')
lines[25] = replaced
replaced = lines[26].replace('NA', '0')
lines[26] = replaced
replaced = lines[27].replace('NA', '0')
lines[27] = replaced
replaced = lines[28].replace('NA', '0')
lines[28] = replaced
replaced = lines[29].replace('NA', '0')
lines[29] = replaced
replaced = lines[30].replace('NA', '0')
lines[30] = replaced
replaced = lines[31].replace('NA', '0')
lines[31] = replaced
replaced = lines[32].replace('NA', '0')
lines[32] = replaced
replaced = lines[33].replace('NA', '0')
lines[33] = replaced
replaced = lines[34].replace('NA', '0')
lines[34] = replaced
replaced = lines[35].replace('NA', '0')
lines[35] = replaced
replaced = lines[36].replace('NA', '0')
lines[36] = replaced
replaced = lines[37].replace('NA', '0')
lines[37] = replaced
replaced = lines[38].replace('NA', '0')
lines[38] = replaced
replaced = lines[39].replace('NA', '0')
lines[39] = replaced
replaced = lines[40].replace('NA', '0')
lines[40] = replaced
replaced = lines[41].replace('NA', '0')
lines[41] = replaced
replaced = lines[42].replace('NA', '0')
lines[42] = replaced
replaced = lines[43].replace('NA', '0')
lines[43] = replaced
replaced = lines[44].replace('NA', '0')
lines[44] = replaced
replaced = lines[45].replace('NA', '0')
lines[45] = replaced
replaced = lines[46].replace('NA', '0')
lines[46] = replaced
replaced = lines[47].replace('NA', '0')
lines[47] = replaced
replaced = lines[48].replace('NA', '0')
lines[48] = replaced
replaced = lines[49].replace('NA', '0')
lines[49] = replaced
replaced = lines[50].replace('NA', '0')
lines[50] = replaced
replaced = lines[51].replace('NA', '0')
lines[51] = replaced
#Testing Purposes
#print (replaced)
#replaced = lines[0:50].replace('NA', '0')
#lines[0:50] = replaced
csv_writer.writerow(lines)









share|improve this question





























    -1















    My Script below is supposed to iterate through files located in this folder, with the ending csv. It reaches each file, but deletes all the data, instead of turning the 'NA' to 0.



    import csv
    import os
    import sys

    #file location
    folderpath = (r'\Deturbansci19cvi2$ClientServicesCC&ACurrentSSOBRD WorkParts OpportunitySample data')
    #print (folderpath)

    for root,dirs,files in os.walk(folderpath, topdown = False):
    for name in files:
    if 'Archive' not in root and name.endswith ('.csv'):
    f = os.path.join(r'\Deturbansci19cvi2$ClientServicesCC&ACurrentSSOBRD WorkParts OpportunitySample data\'+name)
    #print (os.path.join(root, name))
    newfile = (r'\Deturbansci19cvi2$ClientServicesCC&ACurrentSSOBRD WorkParts OpportunitySample data\'+name)
    with open(f, 'rb') as csvfile, open(newfile,'w',newline='') as newcsvfile:
    csv_reader = csv.reader(csvfile, delimiter = ',')
    csv_writer = csv.writer(newcsvfile, delimiter=',')

    for lines in csv_reader:
    #Replacing lines
    replaced = lines[7].replace('NA', '0')
    lines[7] = replaced
    replaced = lines[8].replace('NA', '0')
    lines[8] = replaced
    replaced = lines[17].replace('NA', '0')
    lines[17] = replaced
    replaced = lines[18].replace('NA', '0')
    lines[18] = replaced
    replaced = lines[19].replace('NA', '0')
    lines[19] = replaced
    replaced = lines[20].replace('NA', '0')
    lines[20] = replaced
    replaced = lines[21].replace('NA', '0')
    lines[21] = replaced
    replaced = lines[22].replace('NA', '0')
    lines[22] = replaced
    replaced = lines[23].replace('NA', '0')
    lines[23] = replaced
    replaced = lines[24].replace('NA', '0')
    lines[24] = replaced
    replaced = lines[25].replace('NA', '0')
    lines[25] = replaced
    replaced = lines[26].replace('NA', '0')
    lines[26] = replaced
    replaced = lines[27].replace('NA', '0')
    lines[27] = replaced
    replaced = lines[28].replace('NA', '0')
    lines[28] = replaced
    replaced = lines[29].replace('NA', '0')
    lines[29] = replaced
    replaced = lines[30].replace('NA', '0')
    lines[30] = replaced
    replaced = lines[31].replace('NA', '0')
    lines[31] = replaced
    replaced = lines[32].replace('NA', '0')
    lines[32] = replaced
    replaced = lines[33].replace('NA', '0')
    lines[33] = replaced
    replaced = lines[34].replace('NA', '0')
    lines[34] = replaced
    replaced = lines[35].replace('NA', '0')
    lines[35] = replaced
    replaced = lines[36].replace('NA', '0')
    lines[36] = replaced
    replaced = lines[37].replace('NA', '0')
    lines[37] = replaced
    replaced = lines[38].replace('NA', '0')
    lines[38] = replaced
    replaced = lines[39].replace('NA', '0')
    lines[39] = replaced
    replaced = lines[40].replace('NA', '0')
    lines[40] = replaced
    replaced = lines[41].replace('NA', '0')
    lines[41] = replaced
    replaced = lines[42].replace('NA', '0')
    lines[42] = replaced
    replaced = lines[43].replace('NA', '0')
    lines[43] = replaced
    replaced = lines[44].replace('NA', '0')
    lines[44] = replaced
    replaced = lines[45].replace('NA', '0')
    lines[45] = replaced
    replaced = lines[46].replace('NA', '0')
    lines[46] = replaced
    replaced = lines[47].replace('NA', '0')
    lines[47] = replaced
    replaced = lines[48].replace('NA', '0')
    lines[48] = replaced
    replaced = lines[49].replace('NA', '0')
    lines[49] = replaced
    replaced = lines[50].replace('NA', '0')
    lines[50] = replaced
    replaced = lines[51].replace('NA', '0')
    lines[51] = replaced
    #Testing Purposes
    #print (replaced)
    #replaced = lines[0:50].replace('NA', '0')
    #lines[0:50] = replaced
    csv_writer.writerow(lines)









    share|improve this question



























      -1












      -1








      -1








      My Script below is supposed to iterate through files located in this folder, with the ending csv. It reaches each file, but deletes all the data, instead of turning the 'NA' to 0.



      import csv
      import os
      import sys

      #file location
      folderpath = (r'\Deturbansci19cvi2$ClientServicesCC&ACurrentSSOBRD WorkParts OpportunitySample data')
      #print (folderpath)

      for root,dirs,files in os.walk(folderpath, topdown = False):
      for name in files:
      if 'Archive' not in root and name.endswith ('.csv'):
      f = os.path.join(r'\Deturbansci19cvi2$ClientServicesCC&ACurrentSSOBRD WorkParts OpportunitySample data\'+name)
      #print (os.path.join(root, name))
      newfile = (r'\Deturbansci19cvi2$ClientServicesCC&ACurrentSSOBRD WorkParts OpportunitySample data\'+name)
      with open(f, 'rb') as csvfile, open(newfile,'w',newline='') as newcsvfile:
      csv_reader = csv.reader(csvfile, delimiter = ',')
      csv_writer = csv.writer(newcsvfile, delimiter=',')

      for lines in csv_reader:
      #Replacing lines
      replaced = lines[7].replace('NA', '0')
      lines[7] = replaced
      replaced = lines[8].replace('NA', '0')
      lines[8] = replaced
      replaced = lines[17].replace('NA', '0')
      lines[17] = replaced
      replaced = lines[18].replace('NA', '0')
      lines[18] = replaced
      replaced = lines[19].replace('NA', '0')
      lines[19] = replaced
      replaced = lines[20].replace('NA', '0')
      lines[20] = replaced
      replaced = lines[21].replace('NA', '0')
      lines[21] = replaced
      replaced = lines[22].replace('NA', '0')
      lines[22] = replaced
      replaced = lines[23].replace('NA', '0')
      lines[23] = replaced
      replaced = lines[24].replace('NA', '0')
      lines[24] = replaced
      replaced = lines[25].replace('NA', '0')
      lines[25] = replaced
      replaced = lines[26].replace('NA', '0')
      lines[26] = replaced
      replaced = lines[27].replace('NA', '0')
      lines[27] = replaced
      replaced = lines[28].replace('NA', '0')
      lines[28] = replaced
      replaced = lines[29].replace('NA', '0')
      lines[29] = replaced
      replaced = lines[30].replace('NA', '0')
      lines[30] = replaced
      replaced = lines[31].replace('NA', '0')
      lines[31] = replaced
      replaced = lines[32].replace('NA', '0')
      lines[32] = replaced
      replaced = lines[33].replace('NA', '0')
      lines[33] = replaced
      replaced = lines[34].replace('NA', '0')
      lines[34] = replaced
      replaced = lines[35].replace('NA', '0')
      lines[35] = replaced
      replaced = lines[36].replace('NA', '0')
      lines[36] = replaced
      replaced = lines[37].replace('NA', '0')
      lines[37] = replaced
      replaced = lines[38].replace('NA', '0')
      lines[38] = replaced
      replaced = lines[39].replace('NA', '0')
      lines[39] = replaced
      replaced = lines[40].replace('NA', '0')
      lines[40] = replaced
      replaced = lines[41].replace('NA', '0')
      lines[41] = replaced
      replaced = lines[42].replace('NA', '0')
      lines[42] = replaced
      replaced = lines[43].replace('NA', '0')
      lines[43] = replaced
      replaced = lines[44].replace('NA', '0')
      lines[44] = replaced
      replaced = lines[45].replace('NA', '0')
      lines[45] = replaced
      replaced = lines[46].replace('NA', '0')
      lines[46] = replaced
      replaced = lines[47].replace('NA', '0')
      lines[47] = replaced
      replaced = lines[48].replace('NA', '0')
      lines[48] = replaced
      replaced = lines[49].replace('NA', '0')
      lines[49] = replaced
      replaced = lines[50].replace('NA', '0')
      lines[50] = replaced
      replaced = lines[51].replace('NA', '0')
      lines[51] = replaced
      #Testing Purposes
      #print (replaced)
      #replaced = lines[0:50].replace('NA', '0')
      #lines[0:50] = replaced
      csv_writer.writerow(lines)









      share|improve this question
















      My Script below is supposed to iterate through files located in this folder, with the ending csv. It reaches each file, but deletes all the data, instead of turning the 'NA' to 0.



      import csv
      import os
      import sys

      #file location
      folderpath = (r'\Deturbansci19cvi2$ClientServicesCC&ACurrentSSOBRD WorkParts OpportunitySample data')
      #print (folderpath)

      for root,dirs,files in os.walk(folderpath, topdown = False):
      for name in files:
      if 'Archive' not in root and name.endswith ('.csv'):
      f = os.path.join(r'\Deturbansci19cvi2$ClientServicesCC&ACurrentSSOBRD WorkParts OpportunitySample data\'+name)
      #print (os.path.join(root, name))
      newfile = (r'\Deturbansci19cvi2$ClientServicesCC&ACurrentSSOBRD WorkParts OpportunitySample data\'+name)
      with open(f, 'rb') as csvfile, open(newfile,'w',newline='') as newcsvfile:
      csv_reader = csv.reader(csvfile, delimiter = ',')
      csv_writer = csv.writer(newcsvfile, delimiter=',')

      for lines in csv_reader:
      #Replacing lines
      replaced = lines[7].replace('NA', '0')
      lines[7] = replaced
      replaced = lines[8].replace('NA', '0')
      lines[8] = replaced
      replaced = lines[17].replace('NA', '0')
      lines[17] = replaced
      replaced = lines[18].replace('NA', '0')
      lines[18] = replaced
      replaced = lines[19].replace('NA', '0')
      lines[19] = replaced
      replaced = lines[20].replace('NA', '0')
      lines[20] = replaced
      replaced = lines[21].replace('NA', '0')
      lines[21] = replaced
      replaced = lines[22].replace('NA', '0')
      lines[22] = replaced
      replaced = lines[23].replace('NA', '0')
      lines[23] = replaced
      replaced = lines[24].replace('NA', '0')
      lines[24] = replaced
      replaced = lines[25].replace('NA', '0')
      lines[25] = replaced
      replaced = lines[26].replace('NA', '0')
      lines[26] = replaced
      replaced = lines[27].replace('NA', '0')
      lines[27] = replaced
      replaced = lines[28].replace('NA', '0')
      lines[28] = replaced
      replaced = lines[29].replace('NA', '0')
      lines[29] = replaced
      replaced = lines[30].replace('NA', '0')
      lines[30] = replaced
      replaced = lines[31].replace('NA', '0')
      lines[31] = replaced
      replaced = lines[32].replace('NA', '0')
      lines[32] = replaced
      replaced = lines[33].replace('NA', '0')
      lines[33] = replaced
      replaced = lines[34].replace('NA', '0')
      lines[34] = replaced
      replaced = lines[35].replace('NA', '0')
      lines[35] = replaced
      replaced = lines[36].replace('NA', '0')
      lines[36] = replaced
      replaced = lines[37].replace('NA', '0')
      lines[37] = replaced
      replaced = lines[38].replace('NA', '0')
      lines[38] = replaced
      replaced = lines[39].replace('NA', '0')
      lines[39] = replaced
      replaced = lines[40].replace('NA', '0')
      lines[40] = replaced
      replaced = lines[41].replace('NA', '0')
      lines[41] = replaced
      replaced = lines[42].replace('NA', '0')
      lines[42] = replaced
      replaced = lines[43].replace('NA', '0')
      lines[43] = replaced
      replaced = lines[44].replace('NA', '0')
      lines[44] = replaced
      replaced = lines[45].replace('NA', '0')
      lines[45] = replaced
      replaced = lines[46].replace('NA', '0')
      lines[46] = replaced
      replaced = lines[47].replace('NA', '0')
      lines[47] = replaced
      replaced = lines[48].replace('NA', '0')
      lines[48] = replaced
      replaced = lines[49].replace('NA', '0')
      lines[49] = replaced
      replaced = lines[50].replace('NA', '0')
      lines[50] = replaced
      replaced = lines[51].replace('NA', '0')
      lines[51] = replaced
      #Testing Purposes
      #print (replaced)
      #replaced = lines[0:50].replace('NA', '0')
      #lines[0:50] = replaced
      csv_writer.writerow(lines)






      python csv






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 23:14









      Mark Tolonen

      93.1k12112176




      93.1k12112176










      asked Nov 13 '18 at 20:00









      Stasia YatesStasia Yates

      11




      11
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Your f and newfile are the same path. So in your with statement, you're opening one file for reading, and then opening the same file for writing. Opening the file for writing deletes the file.






          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%2f53288612%2ffile-iteration-returns-no-data-in-files%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









            2














            Your f and newfile are the same path. So in your with statement, you're opening one file for reading, and then opening the same file for writing. Opening the file for writing deletes the file.






            share|improve this answer




























              2














              Your f and newfile are the same path. So in your with statement, you're opening one file for reading, and then opening the same file for writing. Opening the file for writing deletes the file.






              share|improve this answer


























                2












                2








                2







                Your f and newfile are the same path. So in your with statement, you're opening one file for reading, and then opening the same file for writing. Opening the file for writing deletes the file.






                share|improve this answer













                Your f and newfile are the same path. So in your with statement, you're opening one file for reading, and then opening the same file for writing. Opening the file for writing deletes the file.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 20:35









                mRottenmRotten

                25029




                25029






























                    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%2f53288612%2ffile-iteration-returns-no-data-in-files%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

                    Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues