首页 > 解决方案 > 如果在循环文件列表时文件名与 python 中的特定“%y%m%d”格式不匹配,如何继续下一次迭代?

问题描述

我有一些文件,这些文件的名称应该是 '%y%m%d' 格式(不要担心文件扩展名,它可以是任何文件)。我正在遍历文件列表。任何其他这种 '%y%m%d' 格式都应该被忽略,并且循环应该继续下一个文件。

ssh_response = VersaSsh(fqdn(host), user=user, password=pwd, sudo_password=sudo).run_command(**cmds**)

regex = "-rw-r--r-- 1 root root\s+(\d+)\s+([\w\s\d:]+)\s([\dT]+)"
match = re.finditer(regex, ssh_response, re.MULTILINE)
date_list  =[]
if match is not None:
    for matches in match :
          print(matches['somedate']) \\ **this will display the filename.ignore extension**
          date_format=datetime.datetime.strptime(matches['somedate'], '%Y%m%d')
          date = date_format.strftime('%Y-%m-%d')
          date_list.append(date)```

output : 

20200727.0.tar.gz
20200728.0.tar.gz
20200729.0.tar.gz
d2d-vnf5.txt.0.tar.gz // **loop is getting stopped at this point. Instead it should continue with the 
                       next iteration**
d2d-vnf4.txt.0.tar.gz 
20200730.0.tar.gz
20200731.0.tar.gz

 

标签: pythonlinuxterminalpython-behave

解决方案


推荐阅读