【问题标题】:Recursively find the last edit on a file in a folder递归查找文件夹中文件的最后编辑
【发布时间】:2014-05-20 06:24:52
【问题描述】:

所以我正在为一个类构建一个类似于应用程序的保管箱,并且需要检查每个文件以查看自应用程序上次运行以来是否发生了变化。它监视托管在根目录“~/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)

【问题讨论】:

    标签: python ubuntu recursion directory


    【解决方案1】:

    因为~/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.
    

    【讨论】:

      猜你喜欢
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 2012-01-09
      • 2010-10-24
      相关资源
      最近更新 更多