how to open files that match a pattern in a subdirectory












3














Hello i have a problem finding and opening files in a subdirectory.
I have several different files called for example :

mouse_1_animal.txt

mouse_2_animal.txt
mouse_3_animal.txt



so i want to find all these files in the subdirectory of the working dir and open them and do something with there lines. This is my try:



i=1
for path, subdirs, files in os.walk(root) :
for file in files :
if file == "mouse_{0}_animal.txt".format(i) :
#do something
i = i + 1


but apparently it doesn't find all the files so i'm wondering if it's the way i'm using to find the file that is wrong.










share|improve this question






















  • I'd suggest to get a list of all files which match the pattern and then open them, edit and close one by one.
    – Igor T.
    Apr 9 '15 at 9:55






  • 1




    It may be easier to use os.listdir() in case of a single subdirectory.
    – user707650
    Apr 9 '15 at 9:55










  • Note that files contains the filenames relative to the current path where os.walk is at that moment; so you'll need to concatenate the directory and file name (os.path.join()) to get the full path name.
    – user707650
    Apr 9 '15 at 9:58












  • stackoverflow.com/questions/14798220/…
    – Christian K.
    Apr 9 '15 at 9:59
















3














Hello i have a problem finding and opening files in a subdirectory.
I have several different files called for example :

mouse_1_animal.txt

mouse_2_animal.txt
mouse_3_animal.txt



so i want to find all these files in the subdirectory of the working dir and open them and do something with there lines. This is my try:



i=1
for path, subdirs, files in os.walk(root) :
for file in files :
if file == "mouse_{0}_animal.txt".format(i) :
#do something
i = i + 1


but apparently it doesn't find all the files so i'm wondering if it's the way i'm using to find the file that is wrong.










share|improve this question






















  • I'd suggest to get a list of all files which match the pattern and then open them, edit and close one by one.
    – Igor T.
    Apr 9 '15 at 9:55






  • 1




    It may be easier to use os.listdir() in case of a single subdirectory.
    – user707650
    Apr 9 '15 at 9:55










  • Note that files contains the filenames relative to the current path where os.walk is at that moment; so you'll need to concatenate the directory and file name (os.path.join()) to get the full path name.
    – user707650
    Apr 9 '15 at 9:58












  • stackoverflow.com/questions/14798220/…
    – Christian K.
    Apr 9 '15 at 9:59














3












3








3


1





Hello i have a problem finding and opening files in a subdirectory.
I have several different files called for example :

mouse_1_animal.txt

mouse_2_animal.txt
mouse_3_animal.txt



so i want to find all these files in the subdirectory of the working dir and open them and do something with there lines. This is my try:



i=1
for path, subdirs, files in os.walk(root) :
for file in files :
if file == "mouse_{0}_animal.txt".format(i) :
#do something
i = i + 1


but apparently it doesn't find all the files so i'm wondering if it's the way i'm using to find the file that is wrong.










share|improve this question













Hello i have a problem finding and opening files in a subdirectory.
I have several different files called for example :

mouse_1_animal.txt

mouse_2_animal.txt
mouse_3_animal.txt



so i want to find all these files in the subdirectory of the working dir and open them and do something with there lines. This is my try:



i=1
for path, subdirs, files in os.walk(root) :
for file in files :
if file == "mouse_{0}_animal.txt".format(i) :
#do something
i = i + 1


but apparently it doesn't find all the files so i'm wondering if it's the way i'm using to find the file that is wrong.







python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 9 '15 at 9:53









steTsteT

4919




4919












  • I'd suggest to get a list of all files which match the pattern and then open them, edit and close one by one.
    – Igor T.
    Apr 9 '15 at 9:55






  • 1




    It may be easier to use os.listdir() in case of a single subdirectory.
    – user707650
    Apr 9 '15 at 9:55










  • Note that files contains the filenames relative to the current path where os.walk is at that moment; so you'll need to concatenate the directory and file name (os.path.join()) to get the full path name.
    – user707650
    Apr 9 '15 at 9:58












  • stackoverflow.com/questions/14798220/…
    – Christian K.
    Apr 9 '15 at 9:59


















  • I'd suggest to get a list of all files which match the pattern and then open them, edit and close one by one.
    – Igor T.
    Apr 9 '15 at 9:55






  • 1




    It may be easier to use os.listdir() in case of a single subdirectory.
    – user707650
    Apr 9 '15 at 9:55










  • Note that files contains the filenames relative to the current path where os.walk is at that moment; so you'll need to concatenate the directory and file name (os.path.join()) to get the full path name.
    – user707650
    Apr 9 '15 at 9:58












  • stackoverflow.com/questions/14798220/…
    – Christian K.
    Apr 9 '15 at 9:59
















I'd suggest to get a list of all files which match the pattern and then open them, edit and close one by one.
– Igor T.
Apr 9 '15 at 9:55




I'd suggest to get a list of all files which match the pattern and then open them, edit and close one by one.
– Igor T.
Apr 9 '15 at 9:55




1




1




It may be easier to use os.listdir() in case of a single subdirectory.
– user707650
Apr 9 '15 at 9:55




It may be easier to use os.listdir() in case of a single subdirectory.
– user707650
Apr 9 '15 at 9:55












Note that files contains the filenames relative to the current path where os.walk is at that moment; so you'll need to concatenate the directory and file name (os.path.join()) to get the full path name.
– user707650
Apr 9 '15 at 9:58






Note that files contains the filenames relative to the current path where os.walk is at that moment; so you'll need to concatenate the directory and file name (os.path.join()) to get the full path name.
– user707650
Apr 9 '15 at 9:58














stackoverflow.com/questions/14798220/…
– Christian K.
Apr 9 '15 at 9:59




stackoverflow.com/questions/14798220/…
– Christian K.
Apr 9 '15 at 9:59












3 Answers
3






active

oldest

votes


















7














The pythonic way:



import glob
for f in glob.glob('./subDir/mouse_*_animal.txt'):
# do_something





share|improve this answer





























    0














    ok i've solvd my problem like this



     file_list = 
    for name in glob.glob('./subDir/mouse_*_animal.txt'):
    file_list.append(name)
    for i in range(len(file_list)+1):
    if './subDir/mouse_*_animal.txt'.format(i) in file_list:
    #do something





    share|improve this answer

















    • 1




      glob.glob() already returns a list of filenames. So you should shorten your code to: for f in glob.glob('./subDir/mouse_*_animal.txt'): do_something(f)
      – haavee
      Apr 9 '15 at 12:47





















    0














    import fnmatch
    import os
    src = 'sourceDirPath'
    for root, dirnames, filenames in os.walk(src):
    for filename in fnmatch.filter(filenames, 'mouse*.txt'):
    #do something
    i = i + 1


    for older python version you might want to try glob instead of fnmatch






    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%2f29535254%2fhow-to-open-files-that-match-a-pattern-in-a-subdirectory%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      7














      The pythonic way:



      import glob
      for f in glob.glob('./subDir/mouse_*_animal.txt'):
      # do_something





      share|improve this answer


























        7














        The pythonic way:



        import glob
        for f in glob.glob('./subDir/mouse_*_animal.txt'):
        # do_something





        share|improve this answer
























          7












          7








          7






          The pythonic way:



          import glob
          for f in glob.glob('./subDir/mouse_*_animal.txt'):
          # do_something





          share|improve this answer












          The pythonic way:



          import glob
          for f in glob.glob('./subDir/mouse_*_animal.txt'):
          # do_something






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 9 '15 at 12:48









          haaveehaavee

          3,8211620




          3,8211620

























              0














              ok i've solvd my problem like this



               file_list = 
              for name in glob.glob('./subDir/mouse_*_animal.txt'):
              file_list.append(name)
              for i in range(len(file_list)+1):
              if './subDir/mouse_*_animal.txt'.format(i) in file_list:
              #do something





              share|improve this answer

















              • 1




                glob.glob() already returns a list of filenames. So you should shorten your code to: for f in glob.glob('./subDir/mouse_*_animal.txt'): do_something(f)
                – haavee
                Apr 9 '15 at 12:47


















              0














              ok i've solvd my problem like this



               file_list = 
              for name in glob.glob('./subDir/mouse_*_animal.txt'):
              file_list.append(name)
              for i in range(len(file_list)+1):
              if './subDir/mouse_*_animal.txt'.format(i) in file_list:
              #do something





              share|improve this answer

















              • 1




                glob.glob() already returns a list of filenames. So you should shorten your code to: for f in glob.glob('./subDir/mouse_*_animal.txt'): do_something(f)
                – haavee
                Apr 9 '15 at 12:47
















              0












              0








              0






              ok i've solvd my problem like this



               file_list = 
              for name in glob.glob('./subDir/mouse_*_animal.txt'):
              file_list.append(name)
              for i in range(len(file_list)+1):
              if './subDir/mouse_*_animal.txt'.format(i) in file_list:
              #do something





              share|improve this answer












              ok i've solvd my problem like this



               file_list = 
              for name in glob.glob('./subDir/mouse_*_animal.txt'):
              file_list.append(name)
              for i in range(len(file_list)+1):
              if './subDir/mouse_*_animal.txt'.format(i) in file_list:
              #do something






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Apr 9 '15 at 12:43









              steTsteT

              4919




              4919








              • 1




                glob.glob() already returns a list of filenames. So you should shorten your code to: for f in glob.glob('./subDir/mouse_*_animal.txt'): do_something(f)
                – haavee
                Apr 9 '15 at 12:47
















              • 1




                glob.glob() already returns a list of filenames. So you should shorten your code to: for f in glob.glob('./subDir/mouse_*_animal.txt'): do_something(f)
                – haavee
                Apr 9 '15 at 12:47










              1




              1




              glob.glob() already returns a list of filenames. So you should shorten your code to: for f in glob.glob('./subDir/mouse_*_animal.txt'): do_something(f)
              – haavee
              Apr 9 '15 at 12:47






              glob.glob() already returns a list of filenames. So you should shorten your code to: for f in glob.glob('./subDir/mouse_*_animal.txt'): do_something(f)
              – haavee
              Apr 9 '15 at 12:47













              0














              import fnmatch
              import os
              src = 'sourceDirPath'
              for root, dirnames, filenames in os.walk(src):
              for filename in fnmatch.filter(filenames, 'mouse*.txt'):
              #do something
              i = i + 1


              for older python version you might want to try glob instead of fnmatch






              share|improve this answer




























                0














                import fnmatch
                import os
                src = 'sourceDirPath'
                for root, dirnames, filenames in os.walk(src):
                for filename in fnmatch.filter(filenames, 'mouse*.txt'):
                #do something
                i = i + 1


                for older python version you might want to try glob instead of fnmatch






                share|improve this answer


























                  0












                  0








                  0






                  import fnmatch
                  import os
                  src = 'sourceDirPath'
                  for root, dirnames, filenames in os.walk(src):
                  for filename in fnmatch.filter(filenames, 'mouse*.txt'):
                  #do something
                  i = i + 1


                  for older python version you might want to try glob instead of fnmatch






                  share|improve this answer














                  import fnmatch
                  import os
                  src = 'sourceDirPath'
                  for root, dirnames, filenames in os.walk(src):
                  for filename in fnmatch.filter(filenames, 'mouse*.txt'):
                  #do something
                  i = i + 1


                  for older python version you might want to try glob instead of fnmatch







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 18 '18 at 23:45









                  e_i_pi

                  2,56021632




                  2,56021632










                  answered Apr 9 '15 at 10:23









                  kewlkievkewlkiev

                  117




                  117






























                      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%2f29535254%2fhow-to-open-files-that-match-a-pattern-in-a-subdirectory%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

                      How to change which sound is reproduced for terminal bell?

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents