Loop over folders and csv files











up vote
-2
down vote

favorite












I'm trying to write a script in Python to read the last element (bottom right) of .csv files (File001-..-File010) inside N folders (Folder001-..-Folder006) and make some operations (total 10*6 = 60 .csv files). The .csv files have number of rows variable.



My idea for the script:




  • N is the number of folders and P is the number of .csv files inside each folders;

  • Enter the folder 1, enter the P .csv files only to read their last element (bottom right) and write it down in a list (of P elements);

  • Sum all the elements in this list and write the result in the list output (of N elements)

  • Do the same for folder 2 etc..


I would need some help to read the .csv file and its last element within the loop. I read many posts but I am not able to apply them unfortunately.



N = 6
P = 10

def calculate_output(N, P):
output =
for i in range(N):
for j in range(P):
prob =
if FILE NAMES ENDS WITH (".csv") in "./Folder00"+str(i+1):
prob.append(BOTTOM RIGHT ELEMENT OF THE FILE)
output.append(sum(prob[p] for p in range(P)))
return output









share|improve this question




























    up vote
    -2
    down vote

    favorite












    I'm trying to write a script in Python to read the last element (bottom right) of .csv files (File001-..-File010) inside N folders (Folder001-..-Folder006) and make some operations (total 10*6 = 60 .csv files). The .csv files have number of rows variable.



    My idea for the script:




    • N is the number of folders and P is the number of .csv files inside each folders;

    • Enter the folder 1, enter the P .csv files only to read their last element (bottom right) and write it down in a list (of P elements);

    • Sum all the elements in this list and write the result in the list output (of N elements)

    • Do the same for folder 2 etc..


    I would need some help to read the .csv file and its last element within the loop. I read many posts but I am not able to apply them unfortunately.



    N = 6
    P = 10

    def calculate_output(N, P):
    output =
    for i in range(N):
    for j in range(P):
    prob =
    if FILE NAMES ENDS WITH (".csv") in "./Folder00"+str(i+1):
    prob.append(BOTTOM RIGHT ELEMENT OF THE FILE)
    output.append(sum(prob[p] for p in range(P)))
    return output









    share|improve this question


























      up vote
      -2
      down vote

      favorite









      up vote
      -2
      down vote

      favorite











      I'm trying to write a script in Python to read the last element (bottom right) of .csv files (File001-..-File010) inside N folders (Folder001-..-Folder006) and make some operations (total 10*6 = 60 .csv files). The .csv files have number of rows variable.



      My idea for the script:




      • N is the number of folders and P is the number of .csv files inside each folders;

      • Enter the folder 1, enter the P .csv files only to read their last element (bottom right) and write it down in a list (of P elements);

      • Sum all the elements in this list and write the result in the list output (of N elements)

      • Do the same for folder 2 etc..


      I would need some help to read the .csv file and its last element within the loop. I read many posts but I am not able to apply them unfortunately.



      N = 6
      P = 10

      def calculate_output(N, P):
      output =
      for i in range(N):
      for j in range(P):
      prob =
      if FILE NAMES ENDS WITH (".csv") in "./Folder00"+str(i+1):
      prob.append(BOTTOM RIGHT ELEMENT OF THE FILE)
      output.append(sum(prob[p] for p in range(P)))
      return output









      share|improve this question















      I'm trying to write a script in Python to read the last element (bottom right) of .csv files (File001-..-File010) inside N folders (Folder001-..-Folder006) and make some operations (total 10*6 = 60 .csv files). The .csv files have number of rows variable.



      My idea for the script:




      • N is the number of folders and P is the number of .csv files inside each folders;

      • Enter the folder 1, enter the P .csv files only to read their last element (bottom right) and write it down in a list (of P elements);

      • Sum all the elements in this list and write the result in the list output (of N elements)

      • Do the same for folder 2 etc..


      I would need some help to read the .csv file and its last element within the loop. I read many posts but I am not able to apply them unfortunately.



      N = 6
      P = 10

      def calculate_output(N, P):
      output =
      for i in range(N):
      for j in range(P):
      prob =
      if FILE NAMES ENDS WITH (".csv") in "./Folder00"+str(i+1):
      prob.append(BOTTOM RIGHT ELEMENT OF THE FILE)
      output.append(sum(prob[p] for p in range(P)))
      return output






      python loops csv






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 11:40









      tripleee

      86.7k12121175




      86.7k12121175










      asked Nov 8 at 11:29









      ggiordi

      1




      1
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          I'm afraid your question isn't very clear, but I guess you want something like



          import os

          N = 6
          # P = 10 # ????

          def calculate_output(N, P):
          output =
          for i in range(N):
          dirname = "./Folder00" + str(i+1)
          for filename in os.listdir(dirname):
          probsum = 0
          if filename.endswith(".csv"):
          with open(os.path.join(dirname, filename) as csv:
          for line in csv:
          pass
          # line now contains last line
          probsum += int(line.rstrip('n').split(',')[-1])
          output.append(probsum)
          return output


          If you have 10 CSV files in each folder then you don't really need the parameter P for anything; but I'm not entirely sure I guessed correctly what your code is supposed to do here. The above simply takes the last comma-separated field from the last line in each file, and converts from a string to a number. The function returns a list of the sums of the numbers from each folder.



          If the files are huge, maybe look into optimizing the logic for fetching the last line. If you know or can reasonably guess how long the last line can be, seek back from the end of the file that many bytes; see e.g. Get last n lines of a file with Python, similar to tail



          If the CSV format has complications like quoted fields, use csvreader instead of attempting to simply split on comma.






          share|improve this answer




























            up vote
            0
            down vote













            Thank you a lot! I slightly modified your solution in this one:



            N = 6
            def calculate_output(N):
            output =
            for i in range(N):
            prob =
            dirname = "./Folder00" + str(i+1)
            for filename in os.listdir(dirname):
            if filename.endswith(".csv"):
            with open(os.path.join(dirname, filename)) as csv:
            for line in csv:
            pass
            # line now contains last line
            prob.append(int(line.rstrip('n').split(',')[-1]))
            output.append(sum(prob))
            return output


            It is working fine.






            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',
              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%2f53206861%2floop-over-folders-and-csv-files%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
              0
              down vote













              I'm afraid your question isn't very clear, but I guess you want something like



              import os

              N = 6
              # P = 10 # ????

              def calculate_output(N, P):
              output =
              for i in range(N):
              dirname = "./Folder00" + str(i+1)
              for filename in os.listdir(dirname):
              probsum = 0
              if filename.endswith(".csv"):
              with open(os.path.join(dirname, filename) as csv:
              for line in csv:
              pass
              # line now contains last line
              probsum += int(line.rstrip('n').split(',')[-1])
              output.append(probsum)
              return output


              If you have 10 CSV files in each folder then you don't really need the parameter P for anything; but I'm not entirely sure I guessed correctly what your code is supposed to do here. The above simply takes the last comma-separated field from the last line in each file, and converts from a string to a number. The function returns a list of the sums of the numbers from each folder.



              If the files are huge, maybe look into optimizing the logic for fetching the last line. If you know or can reasonably guess how long the last line can be, seek back from the end of the file that many bytes; see e.g. Get last n lines of a file with Python, similar to tail



              If the CSV format has complications like quoted fields, use csvreader instead of attempting to simply split on comma.






              share|improve this answer

























                up vote
                0
                down vote













                I'm afraid your question isn't very clear, but I guess you want something like



                import os

                N = 6
                # P = 10 # ????

                def calculate_output(N, P):
                output =
                for i in range(N):
                dirname = "./Folder00" + str(i+1)
                for filename in os.listdir(dirname):
                probsum = 0
                if filename.endswith(".csv"):
                with open(os.path.join(dirname, filename) as csv:
                for line in csv:
                pass
                # line now contains last line
                probsum += int(line.rstrip('n').split(',')[-1])
                output.append(probsum)
                return output


                If you have 10 CSV files in each folder then you don't really need the parameter P for anything; but I'm not entirely sure I guessed correctly what your code is supposed to do here. The above simply takes the last comma-separated field from the last line in each file, and converts from a string to a number. The function returns a list of the sums of the numbers from each folder.



                If the files are huge, maybe look into optimizing the logic for fetching the last line. If you know or can reasonably guess how long the last line can be, seek back from the end of the file that many bytes; see e.g. Get last n lines of a file with Python, similar to tail



                If the CSV format has complications like quoted fields, use csvreader instead of attempting to simply split on comma.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  I'm afraid your question isn't very clear, but I guess you want something like



                  import os

                  N = 6
                  # P = 10 # ????

                  def calculate_output(N, P):
                  output =
                  for i in range(N):
                  dirname = "./Folder00" + str(i+1)
                  for filename in os.listdir(dirname):
                  probsum = 0
                  if filename.endswith(".csv"):
                  with open(os.path.join(dirname, filename) as csv:
                  for line in csv:
                  pass
                  # line now contains last line
                  probsum += int(line.rstrip('n').split(',')[-1])
                  output.append(probsum)
                  return output


                  If you have 10 CSV files in each folder then you don't really need the parameter P for anything; but I'm not entirely sure I guessed correctly what your code is supposed to do here. The above simply takes the last comma-separated field from the last line in each file, and converts from a string to a number. The function returns a list of the sums of the numbers from each folder.



                  If the files are huge, maybe look into optimizing the logic for fetching the last line. If you know or can reasonably guess how long the last line can be, seek back from the end of the file that many bytes; see e.g. Get last n lines of a file with Python, similar to tail



                  If the CSV format has complications like quoted fields, use csvreader instead of attempting to simply split on comma.






                  share|improve this answer












                  I'm afraid your question isn't very clear, but I guess you want something like



                  import os

                  N = 6
                  # P = 10 # ????

                  def calculate_output(N, P):
                  output =
                  for i in range(N):
                  dirname = "./Folder00" + str(i+1)
                  for filename in os.listdir(dirname):
                  probsum = 0
                  if filename.endswith(".csv"):
                  with open(os.path.join(dirname, filename) as csv:
                  for line in csv:
                  pass
                  # line now contains last line
                  probsum += int(line.rstrip('n').split(',')[-1])
                  output.append(probsum)
                  return output


                  If you have 10 CSV files in each folder then you don't really need the parameter P for anything; but I'm not entirely sure I guessed correctly what your code is supposed to do here. The above simply takes the last comma-separated field from the last line in each file, and converts from a string to a number. The function returns a list of the sums of the numbers from each folder.



                  If the files are huge, maybe look into optimizing the logic for fetching the last line. If you know or can reasonably guess how long the last line can be, seek back from the end of the file that many bytes; see e.g. Get last n lines of a file with Python, similar to tail



                  If the CSV format has complications like quoted fields, use csvreader instead of attempting to simply split on comma.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 8 at 12:24









                  tripleee

                  86.7k12121175




                  86.7k12121175
























                      up vote
                      0
                      down vote













                      Thank you a lot! I slightly modified your solution in this one:



                      N = 6
                      def calculate_output(N):
                      output =
                      for i in range(N):
                      prob =
                      dirname = "./Folder00" + str(i+1)
                      for filename in os.listdir(dirname):
                      if filename.endswith(".csv"):
                      with open(os.path.join(dirname, filename)) as csv:
                      for line in csv:
                      pass
                      # line now contains last line
                      prob.append(int(line.rstrip('n').split(',')[-1]))
                      output.append(sum(prob))
                      return output


                      It is working fine.






                      share|improve this answer

























                        up vote
                        0
                        down vote













                        Thank you a lot! I slightly modified your solution in this one:



                        N = 6
                        def calculate_output(N):
                        output =
                        for i in range(N):
                        prob =
                        dirname = "./Folder00" + str(i+1)
                        for filename in os.listdir(dirname):
                        if filename.endswith(".csv"):
                        with open(os.path.join(dirname, filename)) as csv:
                        for line in csv:
                        pass
                        # line now contains last line
                        prob.append(int(line.rstrip('n').split(',')[-1]))
                        output.append(sum(prob))
                        return output


                        It is working fine.






                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Thank you a lot! I slightly modified your solution in this one:



                          N = 6
                          def calculate_output(N):
                          output =
                          for i in range(N):
                          prob =
                          dirname = "./Folder00" + str(i+1)
                          for filename in os.listdir(dirname):
                          if filename.endswith(".csv"):
                          with open(os.path.join(dirname, filename)) as csv:
                          for line in csv:
                          pass
                          # line now contains last line
                          prob.append(int(line.rstrip('n').split(',')[-1]))
                          output.append(sum(prob))
                          return output


                          It is working fine.






                          share|improve this answer












                          Thank you a lot! I slightly modified your solution in this one:



                          N = 6
                          def calculate_output(N):
                          output =
                          for i in range(N):
                          prob =
                          dirname = "./Folder00" + str(i+1)
                          for filename in os.listdir(dirname):
                          if filename.endswith(".csv"):
                          with open(os.path.join(dirname, filename)) as csv:
                          for line in csv:
                          pass
                          # line now contains last line
                          prob.append(int(line.rstrip('n').split(',')[-1]))
                          output.append(sum(prob))
                          return output


                          It is working fine.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 8 at 14:37









                          ggiordi

                          1




                          1






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206861%2floop-over-folders-and-csv-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

                              Schultheiß

                              Verwaltungsgliederung Dänemarks

                              Liste der Kulturdenkmale in Wilsdruff