Python - 有什么方法可以将相对路径变成绝对路径吗?
current_working_directory = os.getcwd()
relative_directory_of_interest = os.path.join(current_working_directory,"../code/framework/zwave/metadata/")
files = [f for f in os.listdir(relative_directory_of_interest) if os.path.isfile(os.path.join(relative_directory_of_interest,f))]
for f in files:
print(os.path.join(relative_directory_of_interest,f))
输出:
/Users/2Gig/Development/feature_dev/scripts/../code/framework/zwave/metadata/bar.xml
/Users/2Gig/Development/feature_dev/scripts/../code/framework/zwave/metadata/baz.xml
/Users/2Gig/Development/feature_dev/scripts/../code/framework/zwave/metadata/foo.xml
这些文件路径工作正常,但我的一部分希望看到这些是绝对路径(没有任何../ - 我不知道为什么我在乎,也许我是强迫症。我想我也发现记录日志更容易 将它们转换为绝对路径?如果没有的话,没什么大不了的,但稍微谷歌搜索一下就只能找到第三方解决方案。
查看绝对路径。标准 python 库(我在 3.2 上)是否有内置的东西可以 喜欢我想要什么os.path.abspath(file)) 因此上面修改后的源现在看起来像(不是说我没有测试这个,它只是即兴的):
current_working_directory = os.getcwd()
relative_directory_of_interest = os.path.join(current_working_directory,"../code/framework/zwave/metadata/")
files = [f for f in os.listdir(relative_directory_of_interest) if os.path.isfile(os.path.join(relative_directory_of_interest,f))]
for f in files:
print(os.path.abspath(f))
输出:
/Users/2Gig/Development/feature_dev/scripts/ZWave_custom_cmd_classes (08262010).xml /Users/2Gig/Development/feature_dev/scripts/ZWave_custom_cmd_classes (09262010).xml /Users/2Gig/Development/feature_dev/scripts/ZWave_custom_cmd_classes (09262011).xml
current_working_directory = os.getcwd()
relative_directory_of_interest = os.path.join(current_working_directory,"../code/framework/zwave/metadata/")
files = [f for f in os.listdir(relative_directory_of_interest) if os.path.isfile(os.path.join(relative_directory_of_interest,f))]
for f in files:
print(os.path.join(relative_directory_of_interest,f))
Output:
/Users/2Gig/Development/feature_dev/scripts/../code/framework/zwave/metadata/bar.xml
/Users/2Gig/Development/feature_dev/scripts/../code/framework/zwave/metadata/baz.xml
/Users/2Gig/Development/feature_dev/scripts/../code/framework/zwave/metadata/foo.xml
These file paths work fine but part of me would like to see these be absolute paths (without any ../ - I don't know why I care, maybe I'm OCD. I guess I also find it easier in logging to see absolute paths. Is there something built into the standard python libraries (I'm on 3.2) that could turn these into absolute paths? No big deal if not, but a little googling only turned up third-part solutions.
EDIT: Looks like what I want what os.path.abspath(file)) So the modified source could above would now look like (not that I didn't test this, it's just off the cuff):
current_working_directory = os.getcwd()
relative_directory_of_interest = os.path.join(current_working_directory,"../code/framework/zwave/metadata/")
files = [f for f in os.listdir(relative_directory_of_interest) if os.path.isfile(os.path.join(relative_directory_of_interest,f))]
for f in files:
print(os.path.abspath(f))
Output:
/Users/2Gig/Development/feature_dev/scripts/ZWave_custom_cmd_classes (08262010).xml
/Users/2Gig/Development/feature_dev/scripts/ZWave_custom_cmd_classes (09262010).xml
/Users/2Gig/Development/feature_dev/scripts/ZWave_custom_cmd_classes (09262011).xml
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,您的路径已经是绝对路径 - 它们以
/
开头。不过,它们没有标准化,这可以通过os.path.normpath()
。Note that your paths are already absolute -- they start with
/
. They are not normalized, though, which can be done withos.path.normpath()
.