7) b. File Handling Examples




You recently went to Neverland on a vacation with your family and clicked a lot of photos and videos while you were there. After resting for a couple of days you want to look at all the names of the media files that you have clicked. All the media is in /home/vacations in your desktop. Please write a script to list them all.

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

from pathlib import Path
vacation_folder = Path("/home/vacations")

for file_or_folder in vacation_folder.iterdir():
    print(file_or_folder.name)




Nita is a controlling girlfriend who wants her boyfriend to only have her mobile number and no one else’s. She has the mobile phone synced with her laptop and wants to delete all the numbers which are not hers. All the contacts are stored as .cdd file with files named as contact name. These files are stored in /home/contacts. Please write a script to do the same.

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

from pathlib import Path
contacts_folder = Path("/home/contacts")

for file_name in contacts_folder.iterdir():
    if file_name.name != 'Nita.cdd':
        file_name.unlink()




You are the admin of Acme Inc who writes a lot of scripts which involves playing with files. You know that the program finishes by creating shutdown.log file in E: drive. You want to create a module that checks if the file is closed before initiating system shutdown at the end of the day. Write a script to do that.

1
2
3
4
5
6
Program:
#check_shutdown_status.py

f = open('E:/shutdown.log')
if not f.closed:
    f.close()




Kayla is a very smart student and is known in her high school for her python skills. She also helps a lot of her schoolmates in completing their homework by sharing files on school system. Recently her school has started to punish the students who have used school systems for their personal use. Kayla is very worried as her “help” might be punished by school authorities. She wants to delete all her user data completely from her school system but the UI tools are blocked for her. As she is an expert she wants to write a script to delete /home/users/kayla directory. Can you think of what script she might write.

1
2
3
4
5
Program:
#remove_user.py

my_data = Path("/home/users/kayla")
my_data.rmdir()




You are a chemical engineer that is interested in writing about the element Li. You have a set of files in a directory and all the files have the details of some element. The file has chemical formula of the element in the start of the file. You are a weak python developer so want to open one file and print the first 2 characters of the file before running this script over the whole directory. Write the script for the same. All the files are under E: drive named file#.txt.

1
2
3
4
5
6
Program:
#read_2_chars.py

f = open('E:/file1.txt')
print(f.read(2))
f.close()




You manage the firewall department of your company and have created an intercept module that will check if the file is opened in read mode or write mode. You want to check whether the file is opened in read mode or not. If the file is opened in any other mode, you need to throw an alert on the console and the employee will be traced to see as to what he is upto. The task is to crete this module which takes a file object as argument and prints if the mode is not read type. This module should not return anything.

1
2
3
4
5
6
7
8
9
10
11
12
13
Program:
#check_mode.py

def check_mode(file_obj):
    if file_obj.mode != 'r':
        print('The file is not opened in read mode!!!!!!!!!!!!')

f = open('E:/test_file.txt')
check_mode(f)
f.close()
f = open('E:/test_file.txt', 'w')
check_mode(f)
f.close()




Your friend is the CEO of GarbageLtd. This company creates software for people who want to clear their system of junk. As this is your friend’s startup you want to help him in these initial days. He has a very interesting problem with him. He wants to delete all the files in a given directory whose size is greater than 1000MB. He has approached you with this problem and you have decided to write a script for him. As you are also employed, you do not have much time and want to get it over with as quickly as possible. Can you help him in this task. You want to test this by cleaning your system’s E; drive as a test.

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

from pathlib import Path
folder = Path('E:')
for file_name in folder.iterdir():
    if file_name.stat().st_size / 1024 * 1024 > 1000 and file_name.is_file():
        file_name.unlink()