Python Forum
Getting a list of filenames in a directory - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Getting a list of filenames in a directory (/thread-24173.html)



Getting a list of filenames in a directory - DavidHT - Feb-02-2020

Hi all, Python is very new to me, and I'm still finding my feet, so I apologise in advance if this is a daft question. I think this is my first post!

I use an app that downloads podcasts for me. I'm in the middle of writing a Python script to 'work on' the filenames, so that they are formatted the way I want them to be.

What I haven't been able to work out is how I can get a list of all the files in a particular directory, so that I can manipulate the name to the format I want, and then rename the file accordingly.

Any help you can give me would be very much appreciated!

Many thanks.


RE: Getting a list of filenames in a directory - metulburr - Feb-03-2020

import os
for dirpath, dirnames, filenames in os.walk('.'):
    for f in filenames:
        print(f)
'.' is currently directory. Or you can put a path in to a specific directory


RE: Getting a list of filenames in a directory - DavidHT - Feb-03-2020

Thank you! Much appreciated.