Search code examples
pythonglob

I don't understand the 'from' in Python


from glob import glob
from os.path import isfile
def countwords(fp):
   with open(fp) as fh:
       return len(fh.read().split())

print "There are" ,sum(map(countwords, filter(isfile, glob("*.txt") ) ) ), "words in the files."

in the first line, why don't this just simply import glob library?

Is there any reason for using "from glob" in front of "import glob"?


Solution

  • If you write import glob, you would need to use glob.glob.

    from glob import glob takes glob.glob and makes it available as just glob.