Python列表中的for循環,我怎麼都在一個列表Python列表到一個列表
import os
dir="/tmp/logvol/logs/fil/sap/archive/"
n=os.listdir(dir)
#print n
for x in n:
#print x
l = [];
l.append(os.path.join(dir,x))
print l
輸出:
['/tmp/logvol/logs/fil/sap/archive/m.log']
['/tmp/logvol/logs/fil/sap/archive/k.log']
['/tmp/logvol/logs/fil/sap/archive/j.log']
['/tmp/logvol/logs/fil/sap/archive/p.log']
而是多個列表中,我需要所有的一個列表。
['/tmp/logvol/logs/fil/sap/archive/m.log', '/tmp/logvol/logs/fil/sap/archive/k.log', '/tmp/logvol/logs/fil/sap/archive/j.log', '/tmp/logvol/logs/fil/sap/archive/p.log']
你已經關閉了:在for循環之外創建'l'(並且除去無用的結尾分號) – Boud