471,344 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 software developers and data experts.

search a directory/list for a string

46
hey guys,

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

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

the directory could contain:
cool_python_test_01
cool_python_test_02
test
cheese


i want the script to return the:
cool_python_test_01
cool_python_test_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
Jun 15 '07 #1
5 5642
ateale
46
i might be getting there
Expand|Select|Wrap|Line Numbers
  1. mystring = "cool_python_test"
  2.  
  3. list = os.listdir( /adam/test/)
  4. for a in list:
  5.     found = re.findall( mystring + "*", a)
  6.     print found
  7.  
is it possible for items that it returns to display the full file name? - not just the portion of the name that it matches?
Jun 15 '07 #2
ghostdog74
511 Expert 256MB
i might be getting there

mystring = "cool_python_test"

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.
Expand|Select|Wrap|Line Numbers
  1. import glob
  2. if glob.glob(mystring+"*") != []:
  3.    for found in glob.glob(mystring+"*"):
  4.         print found
  5.  
or you can just do normal string finding (not tested)
Expand|Select|Wrap|Line Numbers
  1. thelist = os.listdir("dir")
  2. for found in thelist:
  3.      if found.find(mystring) != -1:
  4.           print found
  5.  
Jun 15 '07 #3
ateale
46
ghostdog74 you are a great help!
The glob method is perfect - will read up more about it

cheers!
Jun 15 '07 #4
ateale
46
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_test_01
cool_python_test_04
cool_python_test_02
cool_python_test_03

i can store "cool_python_test_04" as a string?

thanks for all your help!
Jun 15 '07 #5
ilikepython
844 Expert 512MB
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_test_01
cool_python_test_01
cool_python_test_02
cool_python_test_03

i can store "cool_python_test_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:
Expand|Select|Wrap|Line Numbers
  1. lst = ["cool_python_test_01", "cool_python_test_04", "cool_python_test_02", "cool_python_test_03"]
  2. lst.sort()
  3. print lst[-1]
  4.  
Jun 15 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Ben Fidge | last post: by
1 post views Thread by Prem | last post: by
5 posts views Thread by Dhans | last post: by
3 posts views Thread by Gabe Matteson | last post: by
6 posts views Thread by Joris De Groote | last post: by
14 posts views Thread by henrik.sorensen | last post: by
16 posts views Thread by Computer geek | last post: by
6 posts views Thread by shantanu | last post: by
reply views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.