我嘗試在從 os.scandir() 派生的文件名中找到一個字符串,就像 'ilikecake15.pdf' 或 'ilike1615cake.pdf' 中的字符串 '15' 字符串是唯一的。此代碼的背景:我有幾個文件夾,其中包含需要與列表匹配的 pdf。文件名包含列表中的部分字符串。我認為使用 for 循環可以解決問題,從 txt 中獲取信息。另一個來自 os.scandir() 和 find() 檢查 != -1。故障排除給我留下了兩個 .txt 的值,我嘗試將它們與 s.find(ins) 進行比較。我的目標是整理出 != -1。如果我定義 v1 和 v2 并執行 v1.find(v2) 似乎可以工作,但是在嵌套的 for 循環中,一切都顯示為 -1。(即使我看到正確的匹配)我已經使用 [xx] 從文本文件中刪除了路徑信息,如 G:\thispath\and\thispath。甚至嘗試將 str() 添加到幾乎所有內容中(以防萬一)import ostdpath = 'G:\Somepathwithalotofpdfs'tdfiles = 'G:\Anothersuitablepath/tdfiles.txt'tdlines = 'G:\Anothersuitablepath/tdlines.txt'with os.scandir(tdpath) as pth, open(tdfiles, 'w') as fls: for td in pth: if td.is_file(): fls.write(str(os.fsdecode(td)[51:])) #str and [51:] added to help troubleshoot fls.write('\n')with open(tdlines) as fp, open(tdfiles) as fls: for cnt, line in enumerate(fp): for cmt, lajn in enumerate(fls): print(lajn, line) #just troubleshooting print(str(lajn).find(str(line))) #just troubleshooting if lajn.find(line) != -1: print('KASWSOFJIDFHAIFHSIHFIAHS') # Hlep ples在試圖排除故障時,這已經變成了一個怪物。但是預期的結果是當它從 tdfiles.txt 中的文件 tdlines.txt 找到匹配項時打印后一條語句
添加回答
舉報
0/150
提交
取消