lxml + loads of files = random SerialisationError: IO_WRITE
我正在使用lxml和python 3解析许多文件并合并属于一起的文件。
这些文件实际上成对存储在zip文件中,成对存储(两个文件也首先合并),但我认为这并不重要。
我们正在谈论的是压缩格式的10万个文件,约900MB。
我的问题是我的脚本运行正常,但在某个时候(对于多次运行,它不一定总是在同一点,因此对于某个文件来说应该不是问题)我收到此错误:
File"C:\\Users\\xxx\\workspace\\xxx\\src\\zip2xml.py", line 110, in
_writetonorm
normroot.getroottree().write(norm_file_path) File"lxml.etree.pyx", line 1866, in lxml.etree._ElementTree.write
(src/lxml\\lxml.etree.c:46006) File"serializer.pxi", line 481, in
lxml.etree._tofilelike (src/lxml\\lxml.etree.c:93719) File
"serializer.pxi", line 187, in lxml.etree._raiseSerialisationError
(src/lxml\\lxml.etree.c:90965) lxml.etree.SerialisationError: IO_WRITE
我不知道是什么原因导致此错误。
整个代码有点麻烦,所以我希望相关领域足够:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | def _writetonorm(self, outputpath): '''Writes the current XML to a file. It'll update the file if it already exists and create the file otherwise''' #Find Name name = None try: name = self._xml.xpath("xxx")[0].text.rstrip().lstrip() except Exception as e: try: name = self._xml.xpath("xxx")[0].text.rstrip().lstrip() except Exception as e: name ="damn it!" if name != None: #clean name a bit name = name[:35] table = str.maketrans(' /#*"$!&<>-:.,;()','_________________') name = name.translate(table) name = name.lstrip("_-").rstrip("_-") #generate filename norm_file_name = name +".xml" norm_file_path = os.path.join(outputpath, norm_file_name) #Check if we have that completefile already. If we do, update it. if os.path.isfile(norm_file_path): norm_file = etree.parse(norm_file_path, self._parser) try: normroot = norm_file.getroot() except: print(norm_file_path +"is broken !!!!") time.sleep(10) else: normroot = etree.Element("norm") jurblock = etree.Element("jurblock") self._add_jurblok_attributes(jurblock) jurblock.insert(0, self._xml) normroot.insert(0, jurblock) try: normroot.getroottree().write(norm_file_path) #here the Exception occurs except Exception as e: print(norm_file_path) raise e |
我知道我的异常处理不是很好,但这只是目前的工作证明。
谁能告诉我为什么会发生错误?
查看导致错误的文件,它的格式不正确,但是我怀疑这是因为发生了错误,并且在最近一次迭代之前还可以。
为此,使用映射的网络驱动器似乎是一个错误。 让它在本地处理文件时没有此类异常。
学到了一些东西:)