如何在需要使用 Element Tree 解析的文件名中使用通配符
How to use wild card in filename that needs to be parsed using Element Tree
当我为下面的代码使用通配符时,它会给我一个错误,但是当我使用完整的文件名时,它不会。如何在文件名中使用通配符?
import xml.etree.ElementTree as ET
filename = sys.argv[2]
#file name is 'Data*.xml' as in the future it will change every month so need to use a wild card
tree = ET.parse(filename)
root = tree.getroot()
I get the error below:
OSError: [Errno 22] Invalid argument: 'Data*.xml"
我通过获取当前目录中的所有文件并检查它是否以字符串开头来解决这个问题。
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
if f.startswith(sys.argv[2]):
filename = f
tree = ET.parse(filename)
必须有更简单的方法,我不确定为什么 ET.parse
不能处理通配符。
当我为下面的代码使用通配符时,它会给我一个错误,但是当我使用完整的文件名时,它不会。如何在文件名中使用通配符?
import xml.etree.ElementTree as ET
filename = sys.argv[2]
#file name is 'Data*.xml' as in the future it will change every month so need to use a wild card
tree = ET.parse(filename)
root = tree.getroot()
I get the error below:
OSError: [Errno 22] Invalid argument: 'Data*.xml"
我通过获取当前目录中的所有文件并检查它是否以字符串开头来解决这个问题。
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
if f.startswith(sys.argv[2]):
filename = f
tree = ET.parse(filename)
必须有更简单的方法,我不确定为什么 ET.parse
不能处理通配符。