简体   繁体   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

Would someone be able to point out what am I doing wrong here? 有人可以指出我在这里做错了什么吗? The testIDs = subprocess extracts a list of testIDs(eg: 20150422111035146). testIDs =子进程提取testID的列表(例如:20150422111035146)。 I want to use that as a pattern to see if file exists or not under QMDLPath directory which I have changed through chdir. 我想将其用作模式,以查看通过chdir更改的QMDLPath目录下文件是否存在。 If it is "NOT" present, print that test ID otherwise print a message that no files are missing.The sample file name will be diag-20150422111035146-server1.txt 如果显示为“ NOT”,则打印该测试ID,否则打印一条消息,提示没有文件丢失。示例文件名将为diag-20150422111035146-server1.txt

The problem is that you are reading in a line from the pipe and that line includes the newline. 问题是您正在从管道读取一行,并且该行包括换行符。 When that is included in the glob statement it doesn't match anything. 当它包含在glob语句中时,它与任何内容都不匹配。 You just need to strip the whitespace from the end of the string. 您只需要从字符串末尾去除空格即可。 Replace the for loop line with: 将for循环行替换为:

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

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

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