2012-11-27 52 views
0

這仍然沒有運行,但希望它會給一些更多的信息 限制文件清單:我有這段代碼:從XML文件失敗

#import modules 
import os, sys, datetime, time 
# sys.setdefaultencoding is cancelled by site.py 
reload(sys) # to re-enable sys.setdefaultencoding() 
sys.setdefaultencoding('utf-8') 
try: 
    import xml.etree.cElementTree as ET 
except ImportError: 
    import xml.etree.ElementTree as ET 

now = datetime.datetime.now() 
today = now.strftime("%m/%d/%Y") 
processed = 0 
    #here if sync_list.xml doesn't exist, I ask for some user input i want to save between sessions 
    #then I save that info to sync_list.xml, along with the element to store files already synced 
    root = ET.Element("root") 

    synced = ET.SubElement(root, "synced") 
    synced.set("name", "Already Synced") 
    sfile = ET.SubElement(synced, "sfile") 
    sfile.set("date", today) 
    sfile.text = "firstsync" 

    tree = ET.ElementTree(root) 
    tree.write("sync_list.xml") 
#If sync_list.xml already exists, then I grab the info 
tree = ET.parse("sync_list.xml") 
root = tree.getroot() 
#I pull in all the info I need to work with and: 
for elem in root.findall('sfile'): 
    synced = elem.text 
dcheck = 0 
for elem in root.findall('synced/sfile'): 
    fdate = elem.attrib.get('date') 
    if fdate == today: 
    dcheck += 1 
synced = [elt.text for elt in root.findall('synced/sfile')] 
#if sync_list.xml exists get the list of (UUIDs) $entries that have already been synced, and exclude them from the current query. If no UUID's exist in sync_list.xml, ignore 
synclimit = 10 - dcheck 
print "Already synced today: " + str(dcheck) 
print "Today's synclimit: " + str(synclimit) 
if synclimit == 0: 
    print "Sorry, you've reached your limit for file syncing today. The limit is reset each night at 12:00 a.m." 
    sys.exit() 
synclimit = int(raw_input("How many files do you want to sync today? You have a max amount of " + str(synclimit) + " left today: ")) 

for filename in os.listdir(filepath): 
    if processed >= synclimit: 
     print "You've successfully synced " + str(synclimit) + " files." 
     sys.exit() 
    else: 
     if filename.endswith('.txt') and filename not in synced: 
      filename = os.path.join(filepath, filename) 
      #process the files. This is where I'm getting variable dofilename 
      #The processing works correctly. It's just going over the same files that have already been synced 

      tree = ET.parse('sync_list.xml') 
      synced = tree.find('synced') 
      sfile = ET.SubElement(synced, "sfile", date=today) 
      sfile.text = dofilename 

      tree.write('sync_list.xml', encoding='utf-8', xml_declaration=True) 
      processed += 1 

      print 'Synced ' + dofilename + '....>' 

print 'done!' 

和它是什麼意思做的是檢查sync_list文件名,而不是處理這些文件。

預期輸出: 如果我有一個目錄:

,我跑在第1天的劇本爲5的synclimit,我希望將XML輸出看起來像:

<sfile date="11/26/2012">file1.txt</sfile> 
<sfile date="11/26/2012">file2.txt</sfile> 
<sfile date="11/26/2012">file3.txt</sfile> 
<sfile date="11/26/2012">file4.txt</sfile> 
<sfile date="11/26/2012">file5.txt</sfile> 

這個按預期工作,但如果我在第二天以10的同步限制運行它,我會得到:

<sfile date="11/26/2012">file1.txt</sfile> 
<sfile date="11/26/2012">file2.txt</sfile> 
<sfile date="11/26/2012">file3.txt</sfile> 
<sfile date="11/26/2012">file4.txt</sfile> 
<sfile date="11/26/2012">file5.txt</sfile> 
<sfile date="11/27/2012">file1.txt</sfile> 
<sfile date="11/27/2012">file2.txt</sfile> 
<sfile date="11/27/2012">file3.txt</sfile> 
<sfile date="11/27/2012">file4.txt</sfile> 
<sfile date="11/27/2012">file5.txt</sfile> 
<sfile date="11/27/2012">file6.txt</sfile> 
<sfile date="11/27/2012">file7.txt</sfile> 

我所期待的,會是這樣,無論什麼synclimit設置爲,該腳本將跳過已經被處理過這些文件,而是給我這樣的輸出:

<sfile date="11/26/2012">file1.txt</sfile> 
<sfile date="11/26/2012">file2.txt</sfile> 
<sfile date="11/26/2012">file3.txt</sfile> 
<sfile date="11/26/2012">file4.txt</sfile> 
<sfile date="11/26/2012">file5.txt</sfile> 
<sfile date="11/27/2012">file6.txt</sfile> 
<sfile date="11/27/2012">file7.txt</sfile> 

感恩對於任何指導,關於我誤入歧途的地方。

+0

你的代碼沒有什麼明顯的錯誤,但是它沒有足夠的能夠運行它。你可以發佈一個運行的例子(包括輸入數據和預期輸出)並顯示問題。這將使我們更容易測試它。 – aquavitae

+0

製作一個簡單的文件(例如'done_list.csv'),並在當天存儲一個處理文件列表;然後在程序開始時使用此文件在第二天(將來日期)進行驗證;(例如:您可以驗證已處理的日期爲'26'的任何文件此次不會處理(在'27 'th)..並在每次處理後更新該文件中的信息..。簡單.. – namit

+0

我以爲這就是我在用'if filename.endswith('。txt')和文件名不在同步:我正在檢查sync_list.xml以確保條目尚未同步,如果是,我希望它會跳過它。 – sosukeinu

回答

1

簡單的問題,我只是沒有看到它。將新條目寫入sync_list.xml時,我正在重新定義synced

synced = ET.SubElement(root, "synced") 

將其更改爲不同的變量固定everthing。謝謝。