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

我在第 1 天运行脚本,同步限制为 5,我希望 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>

我希望的是,无论同步限制设置为什么,脚本都会跳过那些已经处理过的文件,而是给我这样的输出:

<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

简单的问题,我只是没有看到它。synced在将新条目写入sync_list.xml 时,我正在重新定义。

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

将其更改为不同的变量可以解决所有问题。谢谢你。

于 2012-11-28T14:37:22.343 回答