簡體   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