2

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

5 Answers 5

3

Try glob:

import glob
print glob.glob('sh.*') #this will give you all sh.* files in the current dir
Sign up to request clarification or add additional context in comments.

1 Comment

or glob.glob('/**/sh*') to get them all
2

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.

Comments

1

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)

3 Comments

The term "basename" does not exclude the extension. See help(os.path.basename).
Thnx for the answer it is printing all files but the name is coming sh.* I want a proper name of a file like sh.txt instead of sh.*.
@glglgl Removing the extension is a lesser-known use of the standard UNIX basename utility, but it is just as valid and standard. Reference here. Note that I didn't link to the python basename utility because I'm not talking about that.
1

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

1 Comment

This would report my.file.pdf when you look for my. Nevertheless, +1 for found = join(root, file)
0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.