LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-24-2011, 01:15 PM   #1
bloodyscript
Member
 
Registered: Apr 2006
Distribution: Sabayon linux 5.1
Posts: 182

Rep: Reputation: 15
python os.walk file search question (un expected duplicate thread)


hi i was wondering if its possible to have pygtk .get_text() specify a filename for os.walk() to find while its traversing the directory tree?

Last edited by bloodyscript; 09-24-2011 at 02:27 PM.
 
Old 09-26-2011, 02:26 AM   #2
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
Quote:
hi i was wondering if its possible to have pygtk .get_text() specify a filename for os.walk() to find while its traversing the directory tree?
This is not only possible but really quite simple to achieve.

I have supplied a little function called locate which you may import from it's module and utilize to return the files found that match the pattern supplied beneath the root directory also supplied. It works by first running os.walk down from the root, getting the files returned and filtering the list with the fnmatch pattern matching module. You may supply a full filename rather than a pattern and this will work too. Note that locate is a generator, a special kind of function, and so may be used in list comprehensions to build a list or "for loops " to iterate through the returned results. Also note I have supplied default parameters so you may call locate without passing anything and it will find all files below the current directory. It may seem unusual to import modules inside a function like this rather than at the head of a module but I have done this so the locate function may be imported directly from it's module and still work. If I performed the imports external to the locate function this would not be possible. As it is locate will return all examples when iterated over that it finds in or below the root directory but if you just want the first found then it's simple to process the first found in the for loop and then break to leave the loop as I do in the example.
Code:
def locate(pattern="*", root=None):
    '''Locate all files matching supplied filename pattern in and below
    supplied root directory.'''
    import os, fnmatch
    if root==None:
        root=os.curdir
    for path, dirs, files in os.walk(os.path.abspath(root)):
        for filename in fnmatch.filter(files, pattern):
            yield os.path.join(path, filename)

if __name__=="__main__":
    myfilepattern="*.py" # pattern to match - change to suit
    myrootdir="/home/ed/source/python" # root dir - this should be changed to suit 
    for names in locate(myfilepattern, myrootdir): 
        print names
        break   # just get first found for this example
    mylist=[names for names in locate(myfilepattern, myrootdir)] # try a list comprehension
    print mylist
It's a simple job to tie in your pygtk code calling get_text to return a filename or pattern and slot this into locate. Also note the locate function is only seven lines of code and so offers little additional code complexity. The majority of the code is just there to test stuff out, offer examples and document.
 
Old 09-26-2011, 10:52 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,642
Blog Entries: 4

Rep: Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933
(oops, never mind)

Nothing to see here... move along... these aren't the 'droids you're looking for...

Last edited by sundialsvcs; 09-26-2011 at 10:53 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python os.walk file search question [SOLVED] bloodyscript Programming 3 09-24-2011 05:09 PM
Python: search for string in a list or file chess Programming 3 08-22-2007 04:22 PM
python gui file dialog question mulciber Programming 0 07-26-2005 07:37 AM
Python Question - moving file across filesystems drj000 Programming 3 01-26-2005 11:36 AM
python call function in same file(very newbie question) rmanocha Programming 2 11-21-2004 12:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration