Search code examples
pythonglobfile-exists

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(e.g: 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. 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


Solution

  • 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. You just need to strip the whitespace from the end of the string. Replace the for loop line with:

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