【问题标题】:Delete directories older than X days?删除超过 X 天的目录?
【发布时间】:2019-07-10 04:53:28
【问题描述】:

我决定为此使用 python,因为我正在学习 Python,所以我尽可能在 Powershell 上使用它。

我对此有理论,但似乎os.stat 不能接受列表,而只能接受字符串或int。现在我只是在去删除之前打印。

import os
import time

path = "\\\\path\\to\\videoroot\\"
now = time.time()
old = now - 1296000

for root, dirs, files in os.walk(path, topdown=False):
    if time.ctime(os.path.getmtime(dirs) < old:
        print (dirs)

输出/错误信息:

return os.stat(filename).st_mtime
    TypeError: argument should be string, bytes or integer, not list

【问题讨论】:

  • 不问为什么,我问如何,但感谢您的建设性意见。
  • 很抱歉有像尼克这样的讨厌的人。我发现问题在于某些人的教育水平低下。所以是的,是一个列表。我也在处理类似的问题。

标签: python


【解决方案1】:

您的代码问题是您将dirs 传递给os.path.getmtime(),而dirslist,正如documentation 中为os.walk 指定的那样

所以你可以通过以下方式解决这个问题:

import os
import time

path = "\\\\path\\to\\videoroot\\"
now = time.time()
old = now - 1296000

for root, dirs, files in os.walk(path, topdown=False):
    for _dir in dirs:
        if os.path.getmtime(_dir) < old:
            print (_dir)

【讨论】:

  • 啊,我知道你在做什么。我会试一试。谢谢!
  • @martimeai 通常您必须验证它是否满足您的需求。在这种情况下确实如此,他的回答已被接受。
  • @sal 谢谢,我明白我做错了什么,很好的例子。
猜你喜欢
  • 2015-04-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-17
相关资源
最近更新 更多