"glob" module in python

Home Forums Python Programming "glob" module in python

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #2053 Reply
    Humble
    Keymaster

    glob module in python is very useful when traversing through a directory structure and when searching for a pattern.. We have “os” module which got methods to achieve the same functionality, how-ever its not straight when glob do..

    >>> import glob
    
    >>> glob.__doc__
    'Filename globbing utility.'
    >>> 
    
    
    glob module itself uses os, re, sys as shown below:
    
    
    
    >>> dir(glob)
    ['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_unicode', 'fnmatch', 'glob', 'glob0', 'glob1', 'has_magic', 'iglob', 'magic_check', 'os', 're', 'sys']
    
    
    
    
    glob's standard syntax is glob(pattern) .
    glob(pattern) returns a list of all files matching a given pattern.
    
    
    
    >>> glob.glob("/home/hchiramm/*.c")
    ['/home/hchiramm/checl.c', '/home/hchiramm/d.c', '/home/hchiramm/ch.c', '/home/hchiramm/h.c']
    
    
    
    As you can see above, glob returns a list which contains the result. how-ever its possible to get a "generator" object from glob by calling its "iglob" method as shown below.
    
    >>> glob.iglob("/home/hchiramm/*.c")
    <generator object iglob at 0x7f6839012910>
    >>> 
    
    
    
Viewing 1 post (of 1 total)
Reply To: "glob" module in python
Your information: