Reputation: 6147
How can I return just the folder leafs within the targeted directory when using os.walk in python.
Here is my function:
def get_files(directory):
directory = os.path.abspath(directory)
if not os.path.isdir(directory):
return False
files = []
for root, subdirs, files in os.walk(directory):
for f in files:
if not f.lower().endswith('.jpg'):
continue
print root, f
get_files('C:/Temp')
Which returns this:
C:\Temp\images socket.jpg
C:\Temp\images vexusrollout.jpg
C:\Temp\newdocs\images utilityrollout.jpg
C:\Temp\newdocs\images vexushelper.jpg
C:\Temp\newdocs\images vexusmtl.jpg
C:\Temp\newdocs\images vexusrollout.jpg
I'd prefer it return this:
images socket.jpg
images vexusrollout.jpg
newdocs\images utilityrollout.jpg
newdocs\images vexushelper.jpg
newdocs\images vexusmtl.jpg
newdocs\images vexusrollout.jpg
The main difference between whatever directory i target for looping through, i don't want that bit to be included in the output.
Upvotes: 0
Views: 61