【发布时间】:2012-03-11 10:32:04
【问题描述】:
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
【问题讨论】:
标签: python file path directory filenames