繁体   English   中英

python glob文件是否存在

[英]python glob file exist or not

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目录下文件是否存在。 如果显示为“ NOT”,则打印该测试ID,否则打印一条消息,提示没有文件丢失。示例文件名将为diag-20150422111035146-server1.txt

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM