472,981 Members | 1,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,981 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 5771
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

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

Similar topics

4
by: Ben Fidge | last post by:
Hi What is the most efficient way to gather a list of files under a given folder recursively with the ability to specify exclusion file masks? For example, say I wanted to get a list of all...
1
by: Prem | last post by:
Hi, I need to search a particular directory for all the files that do not have any extension and have a specific naming convension. The first 3 characters of the file name are alpha and the rest...
5
by: Dhans | last post by:
Framework version : 1.1 For a given directory (which may have subdirectories), I need to identify the number of text files (*.txt). For this I have tried recursive method to search files, it...
3
by: Gabe Matteson | last post by:
I am trying to set the maximum value of the progress bar so that when a user searches through the specified directory they can see their status. the progress bar name is on form2 and is named...
6
by: Joris De Groote | last post by:
Hi, I have a program that must look in in certain folder and take out every file (also the files in subfolders). Now it's no problem to do this with a few for's and if's. However, I don't know...
14
by: henrik.sorensen | last post by:
Hi List, I am looking for a way to do a case insensitive search for file names. Anybody have some hints ? thanks Henrik pl1gcc.sourceforge.net
16
by: Computer geek | last post by:
Hello, I am new to VB.NET and programming in general. I have taught myself a lot of the basics with vb.net but am still quite the novice. I am working on a little application now and I need some...
6
by: shantanu | last post by:
Hi All, I have a requirement to develop a search engine based on some search criteria that will search for the string or statement in all the documents uploaded in the website. The search result...
6
by: tig2810 | last post by:
Hi all This sounds easy and I'm sure it is but i'm just having so much trouble. I have a directory with text files. I want to search the text files for a string and return the name of the file that...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.