shubham Jain
shubham Jain

Reputation: 27

Searching file python

Using this code I want that it search all the files with name sh like sh.c,sh.txt,sh.cpp etc.But this code does not search unless I write lookfor=sh.txt or lookfor=sh.pdf instead of lookfor=sh in the below code. Hence I want that by writing lookfor=sh it searches all files named sh.Please help.

import os
from os.path import join
lookfor = "sh"
for root, dirs, files in os.walk('C:\\'):
    if lookfor in files:
          print "found: %s" % join(root, lookfor)
          break

Upvotes: 2

Views: 759

Answers (5)

glglgl
glglgl

Reputation: 91139

The line

if lookfor in files:

says that the following code should be executed if the list files contains the string given in lookfor.

However, you want that the test should be that the found file name starts with the given string and continues with a ..

Besides, you want the real filename to be determined.

So your code should be

import os
from os.path import join, splitext
lookfor = "sh"
found = None
for root, dirs, files in os.walk('C:\\'):
    for file in files: # test them one after the other
        if splitext(filename)[0] == lookfor:
            found = join(root, file)
            print "found: %s" % found
            break
    if found: break

This can even be improved, as I don't like the way how I break the outer for loop.

Maybe you want to have it as a function:

def look(lookfor, where):
    import os
    from os.path import join, splitext
    for root, dirs, files in os.walk(where):
        for file in files: # test them one after the other
            if splitext(filename)[0] == lookfor:
                found = join(root, file)
                return found

found = look("sh", "C:\\")
if found is not None:
    print "found: %s" % found

Upvotes: 1

kojiro
kojiro

Reputation: 77167

Presumably you want to search for files with sh as their basename. (The part of the name excluding the path and extension.) You can do this with the filter function from the fnmatch module.

import os
from os.path import join
import fnmatch
lookfor = "sh.*"
for root, dirs, files in os.walk('C:\\'):
    for found in fnmatch.filter(files, lookfor):
        print "found: %s" % join(root, found)

Upvotes: 1

Johnny
Johnny

Reputation: 512

Try glob:

import glob
print glob.glob('sh.*') #this will give you all sh.* files in the current dir

Upvotes: 3

bruno desthuilliers
bruno desthuilliers

Reputation: 77942

import os
from os.path import join
lookfor = "sh."
for root, dirs, files in os.walk('C:\\'):
    for filename in files: 
      if filename.startswith(lookfor):
           print "found: %s" % join(root, filename)

You may want to read the doc for fnmatch and glob too.

Upvotes: 0

Thomas Orozco
Thomas Orozco

Reputation: 55303

Replace:

if lookfor in files:

With:

for filename in files:
    if filename.rsplit('.', 1)[0] == lookfor:

What filename.rsplit('.', 1)[0] is remove the rightmost part of the file that's found after a dot (== the extension). In case the file has several dots in it, we keep the rest in the filename.

Upvotes: 2

Related Questions