I want to use os.walk() in Python and I have to give a path argument to it like os.walk(path). In Windows, I do it as os.walk(c:\\).

What os.walk() does is it searches the whole C: directory. In Ubuntu, I want to search the whole home folder so what should be my path and how will I write it in os.walk()?

My code:

import os
from os.path import join

lookfor = "sh.txt"
for root, dirs, files in os.walk('$Home\\'):

    if lookfor in files:
        print "found: %s" % join(root, lookfor)

1 Answer 1


You have 2 choices, pick your favorite.

>>> import os
>>> os.path.expanduser("~")
>>> os.environ["HOME"]
  • os.path.expanduser() is much more portable. Apr 23, 2013 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.