简体   繁体   中英

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). 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

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()+"*"):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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