Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: search entire drive say c:

2 views
Skip to first unread message

Tim Chase

unread,
Feb 12, 2010, 8:01:12 AM2/12/10
to prakash jp, pytho...@python.org
> can any of u help to search a file say "abc.txt" in entire c drive (windows)
> and print the path/s stating such a files presence.

Well, you can just do it from DOS:

c:\> dir /s/b/a abc.txt

Just use os.walk() and check the list of files returned at each
directory-level iteration.

ROOT = "c:\\"
fname = "abc.txt".lower()
for p, d, f in os.walk(ROOT):
for fn in f:
if fn.lower() == fname:
print os.path.join(p, f)
# break

You might also investigate using os.path.normcase() instead of
.lower()

-tkc


Simon Brunning

unread,
Feb 12, 2010, 8:21:26 AM2/12/10
to python-list, prakash jp
On 12 February 2010 12:17, prakash jp <prakas...@gmail.com> wrote:

> can any of u help to search a file say "abc.txt" in entire c drive (windows)
> and print the path/s stating such a files presence.

http://code.activestate.com/recipes/499305/ might be a useful start.

--
Cheers,
Simon B.

McColgst

unread,
Feb 12, 2010, 2:36:46 PM2/12/10
to

>    ROOT = "c:\\"
>    fname = "abc.txt".lower()
>    for p, d, f in os.walk(ROOT):
>      for fn in f:
>        if fn.lower() == fname:
>          print os.path.join(p, f)
>          # break

I think you mean

print os.path.join(p,fn)

-sean

Tim Golden

unread,
Feb 12, 2010, 7:36:42 AM2/12/10
to pytho...@python.org
On 12/02/2010 12:17, prakash jp wrote:
> Hi all,

>
> can any of u help to search a file say "abc.txt" in entire c drive (windows)
> and print the path/s stating such a files presence.

This sounds rather like homework...

Have a look at os.walk

TJG

0 new messages