glob
module helps in finding all paths which match particular patters in Unix shell.
It can handle *
, ?
and the characters expressed in []
.
It can not handle tilde expansion (~ - user home directory) though.
It returns results in arbitrary order than proper sequence.
It supports both relative path matching and absolute path matching as well.
import glob
import sys
print('Creating directory structures and files for experimentation purpose.')
%mkdir folder_l1_1
%mkdir folder_l1_2
%mkdir folder_l1_1/folder_l2
!touch temp.txt temp.jpg temp.png a.png b.jpg c.txt d.txt t1.png t2.jpg .temp.png .temp.jpg
!touch folder_l1_1/t.txt folder_l1_1/t2.png folder_l1_1/a.mp4 folder_l1_1/b.mp4 folder_l1_1/c.mpeg folder_l1_1/t3.jpg
!touch folder_l1_2/t2.txt folder_l1_2/t1.png folder_l1_2/b.mp4 folder_l1_2/c.mp4 folder_l1_2/d.mpeg
!touch folder_l1_1/folder_l2/t.txt folder_l1_1/folder_l2/t2.png folder_l1_1/folder_l2/a.mp4
print('\nCurrent directory contents : ')
%ls
print('\nfolder_l1_1 directory contents : ')
%ls folder_l1_1
print('\nfolder_l1_2 directory contents :')
%ls folder_l1_2
print('\nfolder_l1_1/folder_l2 directory contents : ')
%ls folder_l1_1/folder_l2
glob(pathname, recursive=False)
- Returns all paths which matches pattern. If recursive
is True
with **
then it looks in a subdirectory as well.print(glob.glob('*.txt'))
print(glob.glob('*.png'))
print(glob.glob('*.jpg'))
print(glob.glob('*.*g'))
print(glob.glob('.*'))
print(glob.glob('.*.png'))
print(glob.glob('.*.*g'))
print(glob.glob('[a-z]*.txt'))
print(glob.glob('[a-z]+.txt')) ## This does not work. Only *,? and [] works.
print(glob.glob('[a-z][0-9].*'))
print(glob.glob('t[0-9].*'))
print(glob.glob('[a-z][0-9].*'))
print(glob.glob('[a-z][0-9].*g'))
print(glob.glob('*/*.txt'))
print(glob.glob('*/*/*.txt'))
print(glob.glob('*/*.png'))
print(glob.glob('*/*.*g'))
print(glob.glob('folder_l1_1/*.png'))
print(glob.glob('folder_l1_1/folder_l2/*.*'))
print(glob.glob('folder_l1_2/*'))
print(glob.glob('**/*.txt',recursive=True))
print(glob.glob('**/*.png',recursive=True))
print(glob.glob('**/*.*g',recursive=True))
print(glob.glob('**/*[0-9].txt',recursive=True))
print(glob.glob('*/*/*.txt',recursive=True))
print(glob.glob('*/*/[a-z].txt',recursive=True))
iglob(pathname, recursive=False)
- Returns iterator
of all paths which matches pattern. If recursive
is True
with **
then it looks in a subdirectory as well. It's better to use iterator
when lots of files can match pattern because it won't keep all paths matching in memory. We can avoid memory issues by not keeping a big list in memory.%time normal_dir_list = glob.glob('**/*.*g',recursive=True) ## this one takes more time and memory because it keeps all matching paths in memory after generating.
%time iterator_dir_list = glob.iglob('**/*.*g',recursive=True) ## This takes quite less time as it just generates iterator but does create whole list in memory. Generates element base on call to retrieve element from iterator.
print('Size of list : %d bytes'%sys.getsizeof(normal_dir_list))
print('Size of iterator : %d bytes'%sys.getsizeof(iterator_dir_list))
escape(pathname)
- Escapes all special characters[*,?, [] ]
in pathname
which can be useful if we have special characters are present in pathname
.!touch temp?tea.txt
print(glob.escape('temp?tea.txt'))
print(glob.glob(glob.escape('temp?tea.txt')))
If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.
When going through coding examples, it's quite common to have doubts and errors.
If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.
You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.
If you want to