【发布时间】:2016-12-24 02:17:05
【问题描述】:
我正在尝试遍历目录中的文件并列出每个文件的路径。 我的代码遍历每个文件,但列出了错误的目录。返回完整目录我缺少什么?
到目前为止,这是我的代码:
import os
directory = "posts/"
for file in os.listdir(directory):
if file.endswith(".md"):
dir = os.path.abspath(file)
print "The Path is: " + str(dir)
结构是这样的:
.
├── app.py
└── posts
├── first.md
└── second.md
来自终端的输出(缺少目录的/posts/ 部分):
The Path is: /home/tc/user/python-md-reader/second.md
The Path is: /home/tc/user/python-md-reader/first.md
【问题讨论】: