class __init__:
path = "articles/"
files = os.listdir(path)
files.reverse()
def iterate(Files, Path):
def handleXml(content):
months = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
parse = re.compile('<(.*?)>(.*?)<(.*?)>').findall(content)
day = parse[1][1]
month = months[int(parse[2][1])]
dayN = parse[3][1]
year = parse[4][1]
hour = parse[5][1]
min = parse[6][1]
amPM = parse[7][1]
title = parse[9][1]
author = parse[10][1]
article = parse[11][1]
category = parse[12][1]
if len(Files) > 5:
del Files[5:]
for file in Files:
file = "%s%s" % (Path, file)
f = open(file, 'r')
handleXml(f.read())
f.close()
iterate(files, path)
它在啓動時運行,並且如果我檢查文件數組,它包含所有文件名。 但是,當我循環他們,他們只是不工作,只顯示第一個。 如果我返回文件,我只會得到前兩個,如果我甚至在重複文件中返回解析,它也不相同。 這沒有任何意義。Python循環讀取和解析目錄中的所有文件
我想使用Python做一個簡單的博客,並且因爲我的服務器有一個非常舊的Python版本我不能使用像glob這樣的模塊,所有事情都需要儘可能基本。
files數組包含目錄中的所有文件,這對我來說已經足夠了。我不需要瀏覽articles目錄中的其他目錄。
但是,當我嘗試輸出解析,即使在重複的文件我得到不同的結果。
感謝,
- 湯姆
您發佈的代碼不會調用iterate()函數,也不會打印任何內容。請發佈完整的代碼。 – 2009-08-13 15:35:14
此外,縮進看起來是錯誤的,這在Python中顯然非常重要。請嘗試修復它,使其看起來與您在文本編輯器中的效果完全相同。 – 2009-08-13 15:36:27