Python嵌套的if语句返回错误

时间:2019-03-28 17:05:05

标签: python

我正在尝试读取 .txt .log 文件,搜索特定的单词,然后使用if语句将结果返回到CSV。如何打开其他文件以读取if语句?

我运行了让我在另一个python文件中出现错误的部分,它工作正常并且能够打开和读取。因此,这不是我不认为的代码本身。尽管我刚接触Python。

运行以下命令时,出现以下错误。

  

最新文件=最大值(文件列表清单,密钥= os.path.getctime)   ValueError:max()arg为空序列”

with open('//' + hostName + '/C$/Windows/System32/inetsrv/config/applicationHost.config') as doc:
    lines1 = doc.read()
    if ("logFormat=" + '"W3C"' in lines1):
        list_of_files = glob.glob('//' + hostName + '/C$/inetpub/logs/LogFiles/W3SVC1/*')
        latest_file = max(list_of_files, key=os.path.getctime)
        y = latest_file
        with open(y) as doc1:

1 个答案:

答案 0 :(得分:0)

显然,它适用于大多数主机名。我添加了try和except语句,并捕获了错误。

相关问题