因此,我正在为一个类构建一个类似于保管箱的应用程序,并且需要检查每个文件以查看自该应用程序上次运行以来是否有更改。它监视根目录“~/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.
关于python - 递归查找文件夹中文件的最后一次编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22922095/