所以我正在为一个类构建一个类似应用程序的保管箱,并且需要检查每个文件以查看自上次运行该应用程序以来是否有更改。它监视托管在根目录“~/foo”上的文件夹。我在 Ubuntu 中工作并尝试使用 os.walk 但它甚至没有遍历第一个目录,更不用说找到最后的编辑了。这是我最近尝试的代码
def checkFiles():
for root, dirs, files in os.walk("~/foo"):
print "Got here"
for file in files:
print os.path.join(root, file)
请您参考如下方法:
因为 ~/foo 不是真正的路径名。 (~是shell知道的快捷方式)
你需要使用
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.
