Python - Any way to turn relative paths into absolute paths?
1 2 3 4 5 6 7 | 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)) |
输出:
1 2 3 | /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 |
这些文件路径工作得很好,但我有一部分希望看到这些绝对路径(没有任何../—我不知道为什么我关心,也许我是OCD)。我想我也发现在日志中更容易看到绝对路径。标准的python库(我在3.2上)中是否内置了一些可以将它们转换为绝对路径的东西?如果不是的话没什么大不了的,但是一点点的谷歌搜索只找到了第三部分的解决方案。
编辑:看起来像我想要的os.path.abspath(file)),所以上面修改过的源现在看起来像(不是我没有测试这个,它只是即兴发挥):
1 2 3 4 5 6 7 | 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
请注意,您的路径已经是绝对的——它们从