31

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

codeforester
  • 39,467
  • 16
  • 112
  • 140
user2817200
  • 1,097
  • 2
  • 18
  • 38

2 Answers2

46

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))
Paulo Bu
  • 29,294
  • 6
  • 74
  • 73
  • hm, just to confirm, it should be os.listdir('.') and not os.listdir(dirToScreens)? Because before, I was doing "allSreens = os.listdir(dirToScreens)".. I need a list of files in dirToScreens. – user2817200 Jan 27 '14 at 15:11
  • Sorry, replace `"."` to whatever address you need to `listdir`. I just put `"."` to make a point :). I'll fix it. – Paulo Bu Jan 27 '14 at 15:14
  • 1
    yes, they are just typos. – user2817200 Jan 27 '14 at 15:21
  • 1
    hm, it is giving me empty lists for some reason when I print files.. any idea why? – user2817200 Jan 27 '14 at 15:27
  • Which one? Both of them? – Paulo Bu Jan 27 '14 at 15:27
  • hm yea, both. but I'll leave the answer as marked as correct because it might jus be a problem with another part of my code. Your answer seems correct. – user2817200 Jan 27 '14 at 15:30
  • it's weird becuase when I do "allSreens = os.listdir(dirToScreens)" then it gives all folders and files in the directory so it seems as if filtering just the files should work perfectly fine :/ – user2817200 Jan 27 '14 at 15:31
  • If you want to make sure the method is correct, try it on an interactive interpreter with just a ramdom dir (like '.'). If it works there, then probably there's something wrong in another part of the code. Are you sure you have any file in your directory? Maybe they are all directories. – Paulo Bu Jan 27 '14 at 15:34
  • 1
    This method only works if `dirToScreens` is the current folder, because `os.listdir` returns only the filenames (not the complete path). – hitzg Apr 14 '15 at 09:04
  • @hitzg: The purpose was to answer the question, not to make a generic solution. Adapting it should be easy though by using Python's `abspath()`: http://stackoverflow.com/questions/51520/how-to-get-an-absolute-file-path-in-python – Paulo Bu Apr 14 '15 at 09:07
  • @PauloBu I agree, adapting it is simple. I just felt that stating the limits of the method might be useful for future readers of this post. – hitzg Apr 14 '15 at 09:27
  • 3
    `abspath()` didn't work for me as it is relative to the directory where code is being executed [link here](http://stackoverflow.com/questions/24705679/misunderstanding-of-python-os-path-abspath) . Instead I used: `files = [f for f in os.listdir(dirToScreens) if path.isfile(path.join(dirToScreens, f))]` – Stagg Aug 03 '15 at 10:40
  • http://stackoverflow.com/a/3207973/52074 – Trevor Boyd Smith Oct 17 '16 at 15:21
8

"If you need a list of filenames that all have a certain extension, prefix, or any common string in the middle, use glob instead of writing code to scan the directory contents yourself"

import os
import glob

[name for name in glob.glob(os.path.join(path,'*.*')) if os.path.isfile(os.path.join(path,name))]
juankysmith
  • 11,839
  • 5
  • 37
  • 62
  • 2
    for some reason this is returning an empty list for me... – MattDMo Jan 27 '14 at 15:16
  • @juankysmith hm, it is still giving an empty list for me :/ – user2817200 Jan 27 '14 at 15:28
  • have you tested it after my modification? – juankysmith Jan 27 '14 at 15:31
  • @juankysmith I did "allScreens = [name for name in glob.glob(os.path.join(path,'*.*')) if os.path.isfile(os.path.join(path,name))] print allScreens" and it printed empty lists. I changed it back to "allScreens = os.listdir(dirToScreens) print allScreens" and it printed the list will all files and folders. – user2817200 Jan 27 '14 at 15:34
  • So you are not doing what I recommended to you :) ... glob.glob(os.path.join(path,' * . * ')) ... Be careful copying and pasting – juankysmith Jan 27 '14 at 15:35
  • @juankysmith also, when you say "glob.glob(os.path.join(path,'*.*'))" should I change it to "glob.glob(os.path.join(dirToScreens,'*.*'))"? – user2817200 Jan 27 '14 at 15:36
  • @juankysmith whoops no I did the edit and added the ' * . *' for some reason when I posted on SO it took away the *... also, "os.path.isfile(os.path.join(path,name))]" should be "os.path.isfile(os.path.join(dirToScreens,name))]" for me, right? – user2817200 Jan 27 '14 at 15:39