0

これはそのままでは実行されませんが、もう少し情報が得られることを願ってい ます。次のコードがあります。

#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 をチェックし、それらのファイルを処理しないことです。

期待される出力: ディレクトリがある場合:

/root
  |_ file1.txt
  |_ file2.txt
  |_ file3.txt
  |_ file4.txt
  |_ file5.txt
  |_ file6.txt
  |_ file7.txt

そして、synclimit を 5 にして day1 にスクリプトを実行すると、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 にして 2 日目に実行すると、次のようになります。

<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>

私が迷子になっている場所について、ご指導いただきありがとうございます。

4

1 に答える 1

1

単純な問題、私はそれを見ていませんでした。syncedsync_list.xmlに新しいエントリを書き込むときに再定義していました。

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

これを別の変数に変更すると、すべてが修正されます。ありがとうございました。

于 2012-11-28T14:37:22.343 に答える