7) c. Wildcards and glob Examples




You are a high school student and are preparing for college life. You are understandably very excited for this. You have some time on your hands and have decided to clean up your system. There are some old mp4 files on your system whose backup is already created so you want to delete these files from your system. But before deleting the files you have to list these mp4 files. Write a script that lists the names of all the mp4 files in E:/old_stuff directory of your system.

1
2
3
4
5
6
7
8
Program:
#clean_old.py

from pathlib import Path
folder = Path('E:/old_stuff')
all_mp4 = list(folder.glob('*.mp4'))
for file_name in all_mp4:
    print(file_name)




Your friend has given you a folder in his removable device with his vacation pics. It is not in any particular format and can have nested structure. You want to copy all the jpg files from this folder to your system in C: drive. Please write a script that lists all the files that you would need to copy. The source directory is ‘F:/’.

1
2
3
4
5
6
7
8
Program:
#vacation_pics.py

from pathlib import Path
folder = Path('F:')
all_jpgs = list(folder.glob('**/*.jpg'))
for file_name in all_jpgs:
    print(file_name)




Susan is a research scholar who wants to submit her thesis this semester. She has done quite a lot of work and have saved her files in E:/research_stuff. This folder also consists of some pictures and videos which has helped her in writing those docs. Instead of searching the whole directory one by one she wants to write a script to list all .doc/.docx file and print it on console. Please help her in this.

1
2
3
4
5
6
7
8
Program:
#research_docs_traversal.py

from pathlib import Path
folder = Path('E:/research_stuff')
all_docs = list(folder.glob('*.doc*'))
for file_name in all_docs:
    print(file_name)




Your professor, Brent, is searching through his list of files to get all the data he has of class 5. The way in which he saves his file is class__. He wants to know how many students he has of class 5. His system does not have a sort function due to which he is having a hard time finding out all the data. Please write a script for him that does it and in doing so gain some brownie points from the professor ;). The files are stored in E:/class_data.

1
2
3
4
5
6
7
Program:
#total_students_in_class.py

from pathlib import Path
folder  = Path('E:/class_data')
all_data = list(folder.glob('class_5_*.*'))
print('the total number of files for class 5 is', len(all_data))




Kraken, your friend, wants your help in finding out a file which might help him save his job. He has to submit an important presentation in office tomorrow but does not remember the exact file name. He just know that the file was named in his employee ID,which is 3 digit number, and is present in E: drive. Help him find the exact file name and the path of the file. You want to help and save his job. Please write a program for the same

1
2
3
4
5
6
7
Program:
#find_employee_file.py

from pathlib import Path
folder = Path('E:')
file_list = list(folder.glob('**/' + '[0-9][0-9][0-9]' + '.txt'))
print('The file name is ', file_list[0])




Evil Meaw, a psychotic cat, is on a mission to rule all the humans and has created a software that does the same. You get to know about this from Elektra whose mission is to live in harmony along with all the species. She has told you that Evil Meaw has saved this file with $/n$ in the end and the file is in /home. Write a script to find this file and display the name on console.

1
2
3
4
5
6
7
Program:
#find_cat_file.py

from pathlib import Path
folder = Path('/home')
file_list = list(folder.glob(r'*$/n$.*'))
print('The file name is ', file_list[0])




You are the SEO expert of TailBook Inc and your task includes finding all the tags in an article. There are limited number of tags that are under a folder E:/tailbook. All the tags are instead directories which start with ‘tag_’. As you have to do this task on an everyday basis, create a script which lists all the tags that you can find for the whole site..

1
2
3
4
5
6
7
Program:
#all_tags.py

from pathlib import Path
folder = Path('E:/tailbook')
file_list = list(folder.glob('tag_*'))
print('The total number of tags are ', len(file_list))