Recursive Search in python

12 November 2009 at 4:19 PM | Posted in Python | Leave a comment
Tags:

I do this script to make recursive searches in a specified folder in Ubuntu
Server.I shere it and hope will be usefull to someone:
#! /usr/bin/env python
import os

def search(path,file_name):
 try:
 files = os.listdir(path)
 trunk = 0
 except OSError:
 print “Access Denied: ” + path
 trunk = 1

 if trunk == 0:
 for f in files:
 if os.path.isdir(path + f):
 search(path + f + ‘/’,file_name)
 else:
 if file_name == f:
 print ‘Found : ‘ + path + f

if __name__ == ‘__main__’:
 path = raw_input(”Search Folder: “)
 file = raw_input(”File to Search: “)
 search(path,file)
About these ads

Leave a Comment »

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: