Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)
share|improve this question
Programming questions should be asked on stackoverflow, and '/' is the path separator in unix, not '\'. – psusi Apr 22 '13 at 18:00

You have 2 choices, pick your favorite.

>>> import os
>>> os.path.expanduser("~")
>>> os.environ["HOME"]
share|improve this answer
os.path.expanduser() is much more portable. – Javier Rivera Apr 23 '13 at 15:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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