看板 Python 關於我們 聯絡資訊
透過下方這段來篩選掉0kb的csv檔案 csvfilearr=glob.glob(r'*.csv') dellist=[] i=1 for item in csvfilearr: size = os.path.getsize(item) if size == 0: dellist+=str(i-1) i+=1 else: i+=1 for index in sorted(dellist, reverse=True): del csvfilearr[int(index)] 這樣寫10個檔案內都沒問題,list的 [0,1.....9] 但超過10個檔案就出包了,兩位數的都會被拆成個位數 [0,1....9,1,0,1,1,1,2] 這要怎麼改寫才能變成[0,1....9,10,11,12] 有嘗試dellist+=str(i-1)改成dellist+=int(i-1), 但是會報錯TypeError: 'int' object is not iterable 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 175.181.210.22 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1632516397.A.F86.html
chickengod: https://i.imgur.com/8Tj4D95.jpg 09/25 05:24
chickengod: 用內建的 enumerate() 可以同時得到 index 09/25 05:24
chickengod: 我猜 += 左右 type 要一樣 所以str 被強制轉型成list 09/25 05:37
chickengod: 這裡要放新元素進list 可以用 list.append() 09/25 05:37
chickengod: https://stackoverflow.com/questions/725782 09/25 05:42
s0914714: dellist+=[str(i-1)] 09/25 05:44
lycantrope: csvfilearr = [f for f in glob.glob(r"*.csv") 09/25 09:02
lycantrope: if os.path.getsize > 0] 09/25 09:03
lycantrope: if os.path.getsize(f) > 0] 09/25 09:03
piligo: 感謝chickengod s0914714 lycantrope 三個方法都成功 09/26 23:21
piligo: 程式也越來越短 短到剩一行 看的真清爽 09/26 23:24