I am trying to loop over files in a directory and list the path of each one. My code iterates over each file but lists the wrong directory. What am I missing to return the full directory?
Here's my code so far:
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)
The structure is like this:
.
├── app.py
└── posts
├── first.md
└── second.md
Output from the terminal (missing the /posts/ part of the directory):
The Path is: /home/tc/user/python-md-reader/second.md
The Path is: /home/tc/user/python-md-reader/first.md