Python - 有什么方法可以将相对路径变成绝对路径吗?

发布于 2025-01-07 12:02:07 字数 1540 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怂人 2025-01-14 12:02:07

请注意,您的路径已经是绝对路径 - 它们以/开头。不过,它们没有标准化,这可以通过 os.path.normpath()

Note that your paths are already absolute -- they start with /. They are not normalized, though, which can be done with os.path.normpath().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文