简体   繁体   English

为什么我的脚本无法解析子目录中的xml文件?

[英]Why can't my script parse a xml file in a sub directory?

When I try to parse a xml file in a sub directory I get a FileNotFoundError . 当我尝试解析子目录中的xml文件时,出现FileNotFoundError When I put the file next to the script it can parse it fine. 当我将文件放在脚本旁边时,它可以很好地解析。 But why? 但为什么?

#!/usr/bin/env python3
import os
import xml.etree.ElementTree as ET

script_path = os.path.dirname(os.path.realpath(__file__))

path_to_file = os.path.join(script_path, '/test', 'file.xml')

# works
tree = ET.parse('file.xml')

# Throws file not found error
tree = ET.parse(path_to_file)

Try the easiest possible way of debugging by printing the value of path_to_file. 通过打印path_to_file的值,尝试最简单的调试方法。

os.path.join() is used so you don't have to specify the (os-specific) path separator character for the path it constructs, which means you don't need to (shouldn't) specify them. os.path.join() ,因此您不必为其构造的路径指定(特定于OS的)路径分隔符,这意味着您无需(不应)指定它们。

You are over-specifying the path separator on the test part - change: 您在test零件上过度指定了路径分隔符-更改:

path_to_file = os.path.join(script_path, '/test', 'file.xml')

to

path_to_file = os.path.join(script_path, 'test', 'file.xml')

尝试写'/test'不要加斜杠。

path_to_file = os.path.join(script_path, 'test', 'file.xml')

I am posting the answer myself because although the answers solve the problem, they do not give the correct reason why their code works and mine doesn't. 我自己发布答案,因为尽管答案可以解决问题,但它们并没有给出代码有效而我的代码无效的正确原因。

The significant issue with my code is, that the line 我的代码的重要问题是,该行

path_to_file = os.path.join(script_path, '/test', 'file.xml')
# Expected value of path_to_file:
# /path/to/script/test/file.xml

# Output of print(path_to_file):
# /test/file.xml

contains the absolute path /test instead of the relative path ./test or as pointed out earlier the better way test . 包含绝对路径/test而不是相对路径 ./test或者如先前指出的更好的方法test The result is that with /test , the resulting path will not contain the contents of script_path . 结果是,使用/test ,结果路径将不包含script_path的内容。

The previous answers might help in cases where you go down one directory, but don't cover cases like 在您进入一个目录的情况下,先前的答案可能会有所帮助,但不会涵盖以下情况

path_to_file = os.path.join(script_path, 'test/subdir', 'file.xml')

Where you might find the / useful. 您可能会发现/有用的地方。 I think one can trust os.path.join() to take care of those platform specific directories. 我认为可以相信os.path.join()来照顾那些平台特定的目录。 Please let me know if you don't agree with me one this. 如果您不同意我的一项,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的 Python 脚本在同一目录下找不到 JSON 文件 - My Python Script can't find a JSON file in the same directory Amazon Web Services为什么无法解析我的CloudFront失效批处理请求XML文档? - Why can’t Amazon Web Services parse my CloudFront invalidation batch request XML document? 为什么不能在 Windows 上清理临时目录中的文件处理程序文件? - Why can't my file handler's file within a temporary directory be cleaned up on Windows? 为什么ElementTree无法正确解析此XML标签? - Why ElementTree can't parse this XML tag correctly? 为什么我的脚本不会写入文件? - Why won't my script write to a file? 无法限制我的脚本来解析网页中的特定部分 - Can't limit my script to parse a specific section from a webpage 为什么在我的工作目录(Flask)中看不到创建的数据库文件? - Why can't I see the created database file in my working directory (Flask)? 如何解析具有目录结构的xml文件 - How to parse xml-file with directory structure 为什么 python 脚本找不到文件? - Why can't python script find file? 为什么不能将我的电子邮件下载到本地磁盘以进行解析? - why can't download my email into local disk to parse it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM