searching for words in a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #16
    Originally posted by texas22
    [code=python ]

    import os

    dir_name = r"C:\books"

    word = raw_input("Ente r a word to search for: ")

    entryList = os.listdirs(dir _name)


    for file_name in entryList:

    for line in file(file_name) .readlines():



    if word in line:

    print line

    [/code]

    This is what I have so far when I run it I am getting the error
    "AttributeError : 'module' object has no attribute 'listdirs'

    What does this mean and am I on the right track?
    You are on the correct path. You have an extra space in your closing code tag. It is 'os.listdir' instead of 'os.listdirs'. You need to exclude subdirectories from the file list. The full path and file name must be used to open the files unless the directory is on the Python path. Try this:[code=Python]import os
    dir_name = r'C:\my_directo ry'
    word = raw_input("Ente r a word to search for: ")
    entryList = [os.path.join(di r_name, fn) for fn in os.listdir(dir_ name) if os.path.isfile( os.path.join(di r_name, fn))]

    for file_name in entryList:
    for line in file(file_name) .readlines():
    if word in line:
    print line[/code]

    Comment

    • grantstech
      New Member
      • Jun 2007
      • 16

      #17
      I change the second line below to read the lines but I am getting the error message below.

      Code:
      for file_name in entryList:
          for line in file(file_name).readlines():
              if searchWord in line:
                  print line
      Traceback (most recent call last):
      File "C:/Python25/search.py", line 10, in <module>
      for line in file(file_name) .readlines():
      IOError: [Errno 2] No such file or directory: '1ch'

      I double checked the file address and it is printing the file name out so I think the address is ok. I also opened the file in VIM and it seems ok. What am I doing wrong?

      Comment

      • ghostdog74
        Recognized Expert Contributor
        • Apr 2006
        • 511

        #18
        Originally posted by grantstech
        I change the second line below to read the lines but I am getting the error message below.

        Code:
        for file_name in entryList:
            for line in file(file_name).readlines():
                if searchWord in line:
                    print line
        Traceback (most recent call last):
        File "C:/Python25/search.py", line 10, in <module>
        for line in file(file_name) .readlines():
        IOError: [Errno 2] No such file or directory: '1ch'

        I double checked the file address and it is printing the file name out so I think the address is ok. I also opened the file in VIM and it seems ok. What am I doing wrong?
        check the file names in those directories, whether there are spaces in them.

        Comment

        • grantstech
          New Member
          • Jun 2007
          • 16

          #19
          Originally posted by ghostdog74
          check the file names in those directories, whether there are spaces in them.

          None of the files have a space in the name.

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #20
            Originally posted by grantstech
            None of the files have a space in the name.
            Try this:[code=Python]open(os.path.jo in(dir_name, file_name)).rea dlines()[/code]

            Comment

            • grantstech
              New Member
              • Jun 2007
              • 16

              #21
              Originally posted by bvdet
              Try this:[code=Python]open(os.path.jo in(dir_name, file_name)).rea dlines()[/code]
              I am not sure where to place that so I put it before the loop and got an error that file_name wasn't defined. I placed it inside the loop and it didn't make a difference.

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #22
                Originally posted by grantstech
                I am not sure where to place that so I put it before the loop and got an error that file_name wasn't defined. I placed it inside the loop and it didn't make a difference.
                [code=Python]import os
                dir_name = 'your_directory '
                word = raw_input("Ente r a word to search for: ")
                entryList = [os.path.join(di r_name, fn) for fn in os.listdir(dir_ name) if os.path.isfile( os.path.join(di r_name, fn))]

                for file_name in entryList:
                for line in file(file_name) .readlines():
                if word in line:
                print line[/code]OR[code=Python]import os
                dir_name = r'C:\SDS2_7.0\m acro\New Versions'
                word = raw_input("Ente r a word to search for: ")
                entryList = []
                for fn in os.listdir(dir_ name):
                fn = os.path.join(di r_name, fn)
                if os.path.isfile( fn):
                entryList.appen d(fn)

                # See above for the rest of the code[/code]

                Comment

                • texas22
                  New Member
                  • Jun 2007
                  • 26

                  #23
                  Thanks for all the help guys

                  Comment

                  • grantstech
                    New Member
                    • Jun 2007
                    • 16

                    #24
                    That worked, now I need to work on more than one search word. Thanks!

                    Originally posted by bvdet
                    [code=Python]import os
                    dir_name = 'your_directory '
                    word = raw_input("Ente r a word to search for: ")
                    entryList = [os.path.join(di r_name, fn) for fn in os.listdir(dir_ name) if os.path.isfile( os.path.join(di r_name, fn))]

                    for file_name in entryList:
                    for line in file(file_name) .readlines():
                    if word in line:
                    print line[/code]OR[code=Python]import os
                    dir_name = r'C:\SDS2_7.0\m acro\New Versions'
                    word = raw_input("Ente r a word to search for: ")
                    entryList = []
                    for fn in os.listdir(dir_ name):
                    fn = os.path.join(di r_name, fn)
                    if os.path.isfile( fn):
                    entryList.appen d(fn)

                    # See above for the rest of the code[/code]

                    Comment

                    • bvdet
                      Recognized Expert Specialist
                      • Oct 2006
                      • 2851

                      #25
                      Originally posted by grantstech
                      That worked, now I need to work on more than one search word. Thanks!
                      See post #5 this thread.

                      Comment

                      • bartonc
                        Recognized Expert Expert
                        • Sep 2006
                        • 6478

                        #26
                        Originally posted by grantstech
                        I'm kind of in Texas22's stage here. You all know what you are talking about but it's all Greek to me. You assume we would know what a glob module is when I don't have the concept of what needs to happen. How can we try it? I don't mean to offend and I really appreciate your help but could you simplify your explanation for us python toddlers?
                        As you are new to the Python Forum here on TheScripts (and I really hate to be the "enforcer") , I have allowed your posts to remain in this tread even though (and I hope you can see it, too) it makes quite a mess when there are two people asking questions in the same thread. I'm anxious for this question to be resolved so that you two "toddlers" can start new threads of your own.

                        Please feel free to hit that "start discussion" button any ol' time. And, please, don't jump in on someone else's thread in the future.

                        Thanks.

                        By the way, we actually have some Posting Guidelines which may help you get to know some of the rules and policies better.

                        Comment

                        • grantstech
                          New Member
                          • Jun 2007
                          • 16

                          #27
                          Sorry, I thought this line in the guidelines and thought I should use Texas22's rather than start an identical one.

                          "If you wish to post a question do not post it in a discussion created by someone else unless it is about exactly the same problem."

                          Thanks


                          Originally posted by bartonc
                          As you are new to the Python Forum here on TheScripts (and I really hate to be the "enforcer") , I have allowed your posts to remain in this tread even though (and I hope you can see it, too) it makes quite a mess when there are two people asking questions in the same thread. I'm anxious for this question to be resolved so that you two "toddlers" can start new threads of your own.

                          Please feel free to hit that "start discussion" button any ol' time. And, please, don't jump in on someone else's thread in the future.

                          Thanks.

                          By the way, we actually have some Posting Guidelines which may help you get to know some of the rules and policies better.

                          Comment

                          • bartonc
                            Recognized Expert Expert
                            • Sep 2006
                            • 6478

                            #28
                            Originally posted by grantstech
                            Sorry, I thought this line in the guidelines and thought I should use Texas22's rather than start an identical one.

                            "If you wish to post a question do not post it in a discussion created by someone else unless it is about exactly the same problem."

                            Thanks
                            OK. I'll buy that. No hard feelings, I hope.

                            Comment

                            Working...