Python 解析 XML 存在且文件名有效的文件时出现 OSError 22
Python OSError 22 in parsing XML file that exists and has valid filename
我正在尝试解析从 Bricklink 网站导出的一个或多个 XML
文件。我正在通过 defusedxml
ElementTree 系统打开文件。
我尝试打开的每个文件都会在尝试打开时 OSError 22
,即使文件名看起来有效 - 我可以使用其他工具打开文件并查看内容。
轨迹是:
Traceback (most recent call last):
File "C:\Users\Jason\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "D:/Jason/Projects/Projects/wantedlist_modder.py", line 48, in processfiles
tree = ET.parse(filename)
File "C:\Users\Jason\AppData\Local\Programs\Python\Python38\lib\site-packages\defusedxml\common.py", line 105, in parse
return _parse(source, parser)
File "C:\Users\Jason\AppData\Local\Programs\Python\Python38\lib\xml\etree\ElementTree.py", line 1202, in parse
tree.parse(source, parser)
File "C:\Users\Jason\AppData\Local\Programs\Python\Python38\lib\xml\etree\ElementTree.py", line 584, in parse
source = open(source, "rb")
OSError: [Errno 22] Invalid argument: "'D:/Jason/Downloads/LF_LtN_2020.xml'"
python代码如下:
from tkinter import messagebox, filedialog
import tkinter as tk
from defusedxml.ElementTree import *
import defusedxml.ElementTree as ET
import io
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
self.pack(padx = 20, pady = 20)
frame = Frame(self)
frame.pack(side = TOP)
# create variable for input field
self.filename = tk.StringVar()
self.filename.set("")
# create input field
self.inputfile = tk.Entry(frame)
self.inputfile.pack(side = LEFT)
self.inputfile["textvariable"] = self.filename
# create browse button
self.browse = tk.Button(frame, text = "Browse...", command = self.openbrowse)
self.browse.pack(side = LEFT)
# create process button
self.process = tk.Button(self, text = "Process", command = self.processfiles)
self.process.pack(side = TOP, padx = 20, pady = 20)
# create quit button
self.quit = tk.Button(self, text = "QUIT", fg = "red", command = self.master.destroy)
self.quit.pack(side = BOTTOM)
def openbrowse(self):
filenames = tk.filedialog.askopenfilenames(title = 'select', filetypes = [("XML", ".xml"),])
if not filenames is None:
self.filename.set(', '.join(repr(u) for u in filenames))
def processfiles(self):
print("Roger, roger!")
filenames = tuple(self.filename.get().split(','))
showdone = 0
for filename in filenames:
try:
print(filename)
tree = ET.parse(filename)
showdone = 1
except FileNotFoundError:
tk.messagebox.showinfo("Wanted List Modder", "Please enter a valid filename")
if showdone:
tk.messagebox.showinfo("Wanted List Modder", "Done!")
root = tk.Tk()
app = Application(master=root)
app.mainloop()
我已经进行了大量搜索以尝试修复此问题,但 none 我在本网站或其他地方找到的修复候选者似乎改变了正在发生的事情(例如,我正在解析文件不是字符串)。我不确定内容是否有问题,这似乎与文件名有关 - 或者我将其传递给解析函数的方式。它确实打印了它正在处理的文件的名称,我正在使用浏览按钮系统来定位该文件。我知道这会导致一些额外的处理转换 to/from 元组,但我不认为这是罪魁祸首,因为传递给解析的变量值是一个字符串。
运行 Python 3.8.6
在 Windows 7-x64
机器上使用 defusedxml 0.6.0
... 真的很想帮助确定 OSError 的原因。
提前致谢 - 如果事情很简单,我们深表歉意!我有一段时间没有使用 Python
,我正在与化疗对神经系统的不良影响作斗争...
见下文 - 您在文件名
中有额外的 "
# your code with a bug
try:
with open('"c:/temp/a.xml"', 'rb') as f:
pass
except OSError:
print('we have a problem')
# bug was removed - it works
with open('c:/temp/a.xml', 'rb') as f:
print('it works')
我正在尝试解析从 Bricklink 网站导出的一个或多个 XML
文件。我正在通过 defusedxml
ElementTree 系统打开文件。
我尝试打开的每个文件都会在尝试打开时 OSError 22
,即使文件名看起来有效 - 我可以使用其他工具打开文件并查看内容。
轨迹是:
Traceback (most recent call last):
File "C:\Users\Jason\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "D:/Jason/Projects/Projects/wantedlist_modder.py", line 48, in processfiles
tree = ET.parse(filename)
File "C:\Users\Jason\AppData\Local\Programs\Python\Python38\lib\site-packages\defusedxml\common.py", line 105, in parse
return _parse(source, parser)
File "C:\Users\Jason\AppData\Local\Programs\Python\Python38\lib\xml\etree\ElementTree.py", line 1202, in parse
tree.parse(source, parser)
File "C:\Users\Jason\AppData\Local\Programs\Python\Python38\lib\xml\etree\ElementTree.py", line 584, in parse
source = open(source, "rb")
OSError: [Errno 22] Invalid argument: "'D:/Jason/Downloads/LF_LtN_2020.xml'"
python代码如下:
from tkinter import messagebox, filedialog
import tkinter as tk
from defusedxml.ElementTree import *
import defusedxml.ElementTree as ET
import io
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
self.pack(padx = 20, pady = 20)
frame = Frame(self)
frame.pack(side = TOP)
# create variable for input field
self.filename = tk.StringVar()
self.filename.set("")
# create input field
self.inputfile = tk.Entry(frame)
self.inputfile.pack(side = LEFT)
self.inputfile["textvariable"] = self.filename
# create browse button
self.browse = tk.Button(frame, text = "Browse...", command = self.openbrowse)
self.browse.pack(side = LEFT)
# create process button
self.process = tk.Button(self, text = "Process", command = self.processfiles)
self.process.pack(side = TOP, padx = 20, pady = 20)
# create quit button
self.quit = tk.Button(self, text = "QUIT", fg = "red", command = self.master.destroy)
self.quit.pack(side = BOTTOM)
def openbrowse(self):
filenames = tk.filedialog.askopenfilenames(title = 'select', filetypes = [("XML", ".xml"),])
if not filenames is None:
self.filename.set(', '.join(repr(u) for u in filenames))
def processfiles(self):
print("Roger, roger!")
filenames = tuple(self.filename.get().split(','))
showdone = 0
for filename in filenames:
try:
print(filename)
tree = ET.parse(filename)
showdone = 1
except FileNotFoundError:
tk.messagebox.showinfo("Wanted List Modder", "Please enter a valid filename")
if showdone:
tk.messagebox.showinfo("Wanted List Modder", "Done!")
root = tk.Tk()
app = Application(master=root)
app.mainloop()
我已经进行了大量搜索以尝试修复此问题,但 none 我在本网站或其他地方找到的修复候选者似乎改变了正在发生的事情(例如,我正在解析文件不是字符串)。我不确定内容是否有问题,这似乎与文件名有关 - 或者我将其传递给解析函数的方式。它确实打印了它正在处理的文件的名称,我正在使用浏览按钮系统来定位该文件。我知道这会导致一些额外的处理转换 to/from 元组,但我不认为这是罪魁祸首,因为传递给解析的变量值是一个字符串。
运行 Python 3.8.6
在 Windows 7-x64
机器上使用 defusedxml 0.6.0
... 真的很想帮助确定 OSError 的原因。
提前致谢 - 如果事情很简单,我们深表歉意!我有一段时间没有使用 Python
,我正在与化疗对神经系统的不良影响作斗争...
见下文 - 您在文件名
中有额外的"
# your code with a bug
try:
with open('"c:/temp/a.xml"', 'rb') as f:
pass
except OSError:
print('we have a problem')
# bug was removed - it works
with open('c:/temp/a.xml', 'rb') as f:
print('it works')