How do I list all tga files in a directory (non recursive) in Python?
Question: How do I list all tga files in a directory (non recursive) in Python?


Answer: If you are doing it based on file extension, you can do something like this:

    
    
    import os
    directory = "C:/"
    extension = ".tga"
    list_of_files = [file for file in os.listdir(directory) if file.lower().endswith(extension)]
    

Obviously you can omit the lower() if you can garantee the case of the files.
Also there is the excellent path.py (<http://pypi.python.org/pypi/path.py>)
module.

If you do not know the file extension you can use something like PIL
(<http://www.pythonware.com/products/pil/>) to detect the file type by
decoding the file.

