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(文件))所以上面的修改后的源码现在看起来像(不是我没有测试过这个,它只是在袖口之外):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