【问题标题】:python glob file exist or notpython glob文件是否存在
【发布时间】:2015-07-04 14:08:31
【问题描述】:
testIDs=subprocess.Popen('ls cskpiSummary-310410-LTE-K-M2M-L-*UE1* | cut -d"-" -f7 | uniq', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
try:
        os.chdir(QMDLPath)
except OSError:
        print 'QMDL Directory not found'
        exit()

for testID in testIDs.stdout:
        for file in glob.glob("*"+testID+"*"):
                print file

有人能指出我在这里做错了什么吗? testIDs = 子进程提取一个 testID 列表(例如:20150422111035146)。我想用它作为模式来查看我通过 chdir 更改的 QMDLPath 目录下是否存在文件。如果它“不”存在,则打印该测试 ID,否则打印一条消息,指出没有文件丢失。示例文件名将是 diag-20150422111035146-server1.txt

【问题讨论】:

标签: python glob file-exists


【解决方案1】:

问题是您正在从管道中读取一行,并且该行包含换行符。当它包含在 glob 语句中时,它不匹配任何内容。您只需要从字符串末尾删除空格。将 for 循环行替换为:

for file in glob.glob("*"+testID.rstrip()+"*"):

【讨论】:

  • 成功了。 :) 但这列出了存在的文件。我正在尝试查找文件不存在的那些 testID。
猜你喜欢
  • 2017-06-18
  • 1970-01-01
  • 1970-01-01
  • 2019-06-01
  • 1970-01-01
  • 2017-08-17
  • 2011-06-01
  • 2022-11-15
  • 2016-06-23
相关资源
最近更新 更多