search a directory/list for a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ateale
    New Member
    • Jun 2007
    • 46

    search a directory/list for a string

    hey guys,

    i am looking for a way to list a directory files/folders and find/match a string i define.

    mystring = "cool_python_te st"
    list = os.listdir( /adam/test/)
    matches = re.match( mysting + "*", list)

    the directory could contain:
    cool_python_tes t_01
    cool_python_tes t_02
    test
    cheese


    i want the script to return the:
    cool_python_tes t_01
    cool_python_tes t_02


    any ideas? I googled around and read something about using the re module - can't seem to get my head around it

    any help would be awesome

    cheers!

    Adam
  • ateale
    New Member
    • Jun 2007
    • 46

    #2
    i might be getting there
    [CODE=python]
    mystring = "cool_python_te st"

    list = os.listdir( /adam/test/)
    for a in list:
    found = re.findall( mystring + "*", a)
    print found
    [/CODE]
    is it possible for items that it returns to display the full file name? - not just the portion of the name that it matches?
    Last edited by bartonc; Jun 15 '07, 06:32 PM. Reason: Added [CODE=python][CODE] tags.

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #3
      Originally posted by ateale
      i might be getting there

      mystring = "cool_python_te st"

      list = os.listdir( /adam/test/)
      for a in list:
      found = re.findall( mystring + "*", a)
      print found

      is it possible for items that it returns to display the full file name? - not just the portion of the name that it matches?
      are you trying to find the file/folder names that has the mystring pattern?
      you can use the glob module to find.
      Code:
      import glob
      if glob.glob(mystring+"*") != []:
         for found in glob.glob(mystring+"*"):
              print found
      or you can just do normal string finding (not tested)
      Code:
      thelist = os.listdir("dir")
      for found in thelist:
           if found.find(mystring) != -1:
                print found

      Comment

      • ateale
        New Member
        • Jun 2007
        • 46

        #4
        ghostdog74 you are a great help!
        The glob method is perfect - will read up more about it

        cheers!

        Comment

        • ateale
          New Member
          • Jun 2007
          • 46

          #5
          any ideas on how to find an item in the string with the highest number?

          e.g. if the following 4 files were found

          cool_python_tes t_01
          cool_python_tes t_04
          cool_python_tes t_02
          cool_python_tes t_03

          i can store "cool_python_te st_04" as a string?

          thanks for all your help!

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by ateale
            any ideas on how to find an item in the string with the highest number?

            e.g. if the following 4 files were found

            cool_python_tes t_01
            cool_python_tes t_01
            cool_python_tes t_02
            cool_python_tes t_03

            i can store "cool_python_te st_04" as a string?

            thanks for all your help!
            You can sort them, but that might not be the best way, depending on what you are doing:
            [code=python]
            lst = ["cool_python_te st_01", "cool_python_te st_04", "cool_python_te st_02", "cool_python_te st_03"]
            lst.sort()
            print lst[-1]
            [/code]

            Comment

            Working...