Mat*_*und 3 python directory filenames file path
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))
Run Code Online (Sandbox Code Playgroud)
输出:
/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
Run Code Online (Sandbox Code Playgroud)
这些文件路径工作正常,但我想看到这些是绝对路径(没有任何../ - 我不知道为什么我关心,也许我是强迫症.我想我也觉得在登录时更容易看到标准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))
Run Code Online (Sandbox Code Playgroud)
输出:
/ 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
Sve*_*ach 10
请注意,你的路都已经是绝对的-他们下手/.但是,它们不是标准化的,可以用它来完成os.path.normpath().