需要Python删除旧目录的帮助吗

2024-10-02 02:34:14 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我在python中用来删除旧目录的函数

def delete_olddirs(days,file_path):
     numdays = 60*60*24*days
     now = time.time()

     for dir in os.listdir(file_path):
              r = file_path
              timestamp = os.path.getmtime(os.path.join(r,dir))
              if now-numdays > timestamp:
                  try:
                       print "removing ",os.path.join(r,dir)
                       #shutil.rmtree(os.path.join(r,dir))  #uncomment to use
                  except Exception,e:
                       print e
                       pass
                  else:
                       print "some message for success"

问题是每次我看到消息removing . ....我也会看到消息

some message for success

我想知道为什么每次都要执行else部分


Tags: pathfortimeosdirsomedayselse
3条回答

如果没有引发异常,则执行elsetry部分。你知道吗

http://docs.python.org/reference/compound_stmts.html#the-try-statement

The optional else clause is executed if and when control flows off the end of the try clause. [2] Exceptions in the else clause are not handled by the preceding except clauses.

似乎很清楚。你知道吗

另外一点是成功,你误解了它的目的。 见:http://docs.python.org/tutorial/errors.html

相关问题 更多 >

    热门问题