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))
输出:Python - 任何将相对路径转换为绝对路径的方法?
/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))
输出:
/用户/ 2Gig /开发/ feature_dev /脚本/ ZWave_custom_cmd_classes(08262010).XML /用户/ 2Gig /开发/ feature_dev /脚本/ ZWave_custom_cmd_classes(09262010).XML /用户/ 2Gig /开发/ feature_dev /脚本/ ZWave_custom_cmd_classes(09262011).XML
[如何获取Python中的绝对文件路径]可能的重复(https://stackoverflow.com/questions/51520/how-to-get-an-absolute-file-path-in-python) – 2017-07-14 13:20:29