How to get only files in directory?

file, python, python-2.7

Solution

You can use `os.path.isfile` method:

import os
from os import path
files = [f for f in os.listdir(dirToScreens) if path.isfile(f)]

Or if you feel functional :D

files = filter(path.isfile, os.listdir(dirToScreens))

Problem

I have this code: ``` allFiles = os.listdir(myPath) for module in allFiles: if 'Module' in module: #if the word module is in the filename dirToScreens = os.path.join(myPath, module) allSreens = os.listdir(dirToScreens) ``` Now, all works well, I just need to change the line ``` allSreens = os.listdir(dirToScreens) ``` to get a list of just files, not folders. Therefore, when I use ``` allScreens [ f for f in os.listdir(dirToScreens) if os.isfile(join(dirToScreens, f)) ] ``` it says ``` module object has no attribute isfile ``` NOTE: I am using Python 2.7

Original source

Related problems