<jupyter_start><jupyter_text>**Name : <NAME>****Class : SE COMPS B****Roll No. : 8960****Title : Python Experiment 3** **Aim: Write python programs to understand different file handling operations.**1. Write a python program which will count the spaces, tabs, and lines in any given text file. Check if the file exists. If file does not exists then handle that exception.**Code :**<jupyter_code>from google.colab import drive
drive.mount('/content/drive')
from collections import Counter
try:
  with open("drive/MyDrive/8960_Candida_Python/Practical 3/content.txt") as f:
    text = f.read()
    count = Counter(text)
except:
  print("File not Found")
else:
 spaces = count[" "]
 tabs = count["\t"]
 lines = count["\n"]
 print("The total number of spaces present in this file is ", spaces)
 print("The total number of tabs present in this file is ", tabs)
 print("The total number of lines present in this file is ", lines)<jupyter_output>The total number of spaces present in this file is  114
The total number of tabs present in this file is  5
The total number of lines present in this file is  6<jupyter_text>2. The file cities_and_times.txt contains city names and times.Each line contains the name of the city, followed by the name of the day ("Sun") and the time in the form hh:mm. Read in the file and create an alphabetically ordered list of the form[('Amsterdam', 'Sun', (8, 52)), ('Anchorage', 'Sat', (23, 52)), ('Ankara', 'Sun', (10, 52)), ('Athens', 'Sun', (9, 52)), ('Atlanta', 'Sun', (2, 52)), ('Auckland', 'Sun', (20, 52)), ('Barcelona', 'Sun', (8, 52)), ('Beirut', 'Sun', (9, 52)), ...….('Toronto', 'Sun', (2, 52)), ('Vancouver', 'Sun', (0, 52)), ('Vienna', 'Sun', (8, 52)), ('Warsaw', 'Sun', (8, 52)), ('Washington DC', 'Sun', (2, 52)), ('Winnipeg', 'Sun', (1, 52)), ('Zurich', 'Sun', (8, 52))]Finally, the list should be dumped for later usage with the pickle module.**Code :**<jupyter_code>import pickle
l=[tuple(line.strip().split(" ")) for line in open("drive/MyDrive/8960_Candida_Python/Practical 3/cities_and_times.txt")]
l.sort()
dump=open("drive/MyDrive/8960_Candida_Python/Practical 3/dump","wb")
pickle.dump(l,dump)
dump.close()

load=open("drive/MyDrive/8960_Candida_Python/Practical 3/dump","rb")
print(*pickle.load(load) , sep = '\n')<jupyter_output>('Amsterdam', 'Sun', '08:52')
('Anchorage', 'Sat', '23:52')
('Ankara', 'Sun', '10:52')
('Athens', 'Sun', '09:52')
('Atlanta', 'Sun', '02:52')
('Auckland', 'Sun', '20:52')
('Barcelona', 'Sun', '08:52')
('Beirut', 'Sun', '09:52')
('Berlin', 'Sun', '08:52')
('Boston', 'Sun', '02:52')
('Brasilia', 'Sun', '05:52')
('Brussels', 'Sun', '08:52')
('Bucharest', 'Sun', '09:52')
('Budapest', 'Sun', '08:52')
('Cairo', 'Sun', '09:52')
('Calgary', 'Sun', '01:52')
('Cape', 'Town', 'Sun', '09:52')
('Casablanca', 'Sun', '07:52')
('Chicago', 'Sun', '01:52')
('Columbus', 'Sun', '02:52')
('Copenhagen', 'Sun', '08:52')
('Dallas', 'Sun', '01:52')
('Denver', 'Sun', '01:52')
('Detroit', 'Sun', '02:52')
('Dubai', 'Sun', '11:52')
('Dublin', 'Sun', '07:52')
('Edmonton', 'Sun', '01:52')
('Frankfurt', 'Sun', '08:52')
('Halifax', 'Sun', '03:52')
('Helsinki', 'Sun', '09:52')
('Houston', 'Sun', '01:52')
('Indianapolis', 'Sun', '02:52')
('Istanbul', 'Sun', '10:52')
('Jerusalem', 'Sun', '09:52')
('Johannesburg', 'Sun', [...]<jupyter_text>**Post Labs**<jupyter_code>def longestword(filename):
  with open("drive/MyDrive/8960_Candida_Python/Practical 3/computer.txt") as f:
    words = f.read().split()
    max_len_word = max(words,key=len)
    max_len = len(max(words,key=len))
    print('The word in the file with maximum length is :',max_len_word)
    print('And its length is : ',max_len)
longestword("text.txt")<jupyter_output>The word in the file with maximum length is : automatically.
And its length is :  14
{'fraction_non_alphanumeric': 0.2221927966101695, 'fraction_numerical': 0.05905720338983051, 'mean_word_length': 5.83001808318264, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '448766', 'license': 'Apache-2.0', 'n_tokens_mistral': 1553, 'n_tokens_neox': 1365, 'n_words': 427, 'path': '8960_Candida_Practical_3/8960 Candida - Practical 3.ipynb', 'repo_name': 'Candida18/Python', 'size': '3790'}