473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 5789
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.