Question

So I'm building a dropbox like app for a class and need to check every file to see if there has been a change since the app was last run. It watches a folder hosted on the root directory "~/foo". I'm working in Ubuntu and was trying to use os.walk but it wasn't even traversing the first directory, let alone find the last edits. This was the most recent code I tried

def checkFiles():
for root, dirs, files in os.walk("~/foo"):
    print "Got here"
    for file in files:
        print os.path.join(root, file)
Was it helpful?

Solution

Because ~/foo isn't the real path name. (~ is a shortcut that the shell knows)

you need to use

os.path.expanduser("~/foo")

>>> help(os.path.expanduser)

Help on function expanduser in module posixpath:

expanduser(path)
    Expand ~ and ~user constructions.  If user or $HOME is unknown,
    do nothing.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top