Unique words in a file sorted alphabetically

 # Unique words in a file


fp = None


try:

fp = open("fileipt.txt","r")

except IOError as e:

print("File opening error : ", e)

except:

print("unknown error")


else:

fdata = fp.read()

fdata = fdata.replace('.',"")

fdata = fdata.replace('-',"")

fdata = fdata.replace(',',"")

fdata = fdata.lower()

Words = fdata.split()

UniqueWords = list(set(Words))

list.sort(UniqueWords)

print("Number of Unique Words = ",len(UniqueWords))

print("Unique Words:")

print(UniqueWords)

finally:

if(fp):

fp.close()


"""

Sample input

Input file: fileipt.txt


File handling in Python has several functions for creating, reading, updating, and deleting files. Read - Default value. Opens a file for reading, error if the file does not exist. Append - Opens a file for appending, creates the file if it does not exist. Write - Opens a file for writing, creates the file if it does not exist. Create - Creates the specified file, returns an error if the file exists.


>python File9.py

Number of Unique Words =  36

Unique Words:

['a', 'an', 'and', 'append', 'appending', 'create', 'creates', 'creating', 'default', 'deleting', 'does', 'error', 'exist', 'exists', 'file', 'files', 'for', 'functions', 'handling', 'has', 'if', 'in', 'it', 'not', 'opens', 'python', 'read', 'reading', 'returns', 'several', 'specified', 'the', 'updating', 'value', 'write', 'writing']


"""



Modules - Arithmetic operations

# Creating modules 

# Arithmetic operations 


# arithopns.py


def fnadd(a,b):

return a+b


def fnsub(a,b):

return a-b


def fnmul(a,b):

return a*b


def fndiv(a,b):

return a/b


def fnmod(a,b):

return a%b


def fntruncdiv(a,b):

return a//b




# Main program

# importing modules 


from arithopns import *


a = eval(input("Enter value: "))

b = eval(input("Enter value: "))


print("Sum =",fnadd(a,b))

print("Difference =",fnsub(a,b))

print("Product =",fnmul(a,b))

print("Division =",round(fndiv(a,b),4))

print("Modulo reminder =",fnmod(a,b))

print("Truncation division =",fntruncdiv(a,b))


"""

Sample output

>python modpgmAO.py

Enter value: 7

Enter value: 6

Sum = 13

Difference = 1

Product = 42

Division = 1.1667

Modulo reminder = 1

Truncation division = 1


"""