Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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)
        break
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
1  
add comment

1 Answer

You have 2 choices, pick your favorite.

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

Your Answer

 
discard

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.