#PYTHON-TEXT FILE OPERATIONS:
#TASK 1, 18/11/2024
'''CREATE AND Write Data to a text file'''
#SOHAM SURANA, S7C
'''def APPEND():
with open("EG.txt","a") as f1:
S=input("Enter text to write into file: ")
f1.write(S)
print("Data written to file successfully.")
APPEND()'''
#_____________________________________________________________________________
#TASK 2, 18/11/2024
'''Display data from a text file'''
#SOHAM SURANA, S7C
'''def DISPLAY():
try:
with open("EG.txt","r") as f1:
D=f1.read()
print("File Content:")
print(D)
except:
print("File not found.")
DISPLAY()'''
#_____________________________________________________________________________
#TASK 3, 18/11/2024
'''
def DELETE():
F=0
try:
with open("EG.txt",'r') as f1:
OC=f1.read()
L=OC.split()
except:
print("File not Found!")
L1=[]
W=input("Enter the word you would like to delete from the file: ")
try:
with open("EG.txt",'w') as f2:
for i in L:
if i==W:
F=1
else:
L1.append(i)
NS=' '.join(L1)
f2.write(NS)
if F==0:
print("No such word found in the file!")
else:
print("Data deleted successfully!")
except:
print("Deletion error!")
DELETE()'''
#_____________________________________________________________________________
#TASK 4, 19/11/2024
'''Read a text file line by line and display each word separated by a # '''
#SOHAM SURANA, S7C
'''#ADDING INITIAL TEXT TO THE FILE
f1=open('test.txt','w')
S="hello world"
f1.write(S)
f1.close()
#FUNCTION TO PRINT '#' AFTER EACH WORD OF THE TEXT FILE
def R(f):
try:
with open(f,'r') as f1:
L=f1.read()
L1=L.split()
for i in L1:
print(i+"#",end="")
except:
print("File not found.")
# Call the function with the file name
N=input("Enter name of the file: ")
R(N)'''
#_____________________________________________________________________________
#TASK 5, 19/11/2024
'''Remove all the words that contain the character `a' in a text file and write it to another text file'''
#SOHAM SURANA, S7C
'''def DELETEA(f1,f2):
try:
with open(f1,'r') as f3:
L=f3.readlines()
with open(f2,'w') as f4:
for i in L:
W=i.split()
RW=[]
for j in W:
if 'a' not in j and 'A' not in j:
RW.append(j)
f4.write(' '.join(RW)+'\n')
print("Filtered content written to",f2)
except:
print("File not found.")
# Call the function with input and output file names
DELETEA("eg1.txt", "NewOutput.txt")'''
#_____________________________________________________________________________
#TASK 6, 19/11/2024
'''Read a text file and display the number of vowels/ consonants/uppercase characters in the file. '''
#SOHAM SURANA, S7C
'''
def COUNT(f):
vowels='aeiouAEIOU'
VC=0 # Vowel Count
CC=0 # Consonant Count
UC=0 # Uppercase Count
try:
with open(f,'r') as f1:
text=f1.read()
# Loop through each character in the text
for i in text:
if i in vowels:
VC+=1
elif i.isalpha() and i not in vowels:
CC+=1
if i.isupper():
UC += 1
print("Vowels:",VC)
print("Consonants:",CC)
print("Uppercase Characters:",UC)
except:
print("File not found.")
# Call the function with the file name
COUNT("eg1.txt")'''
#_____________________________________________________________________________
#TASK 7, 19/11/2024
'''Menu-based student record management system'''
#SOHAM SURANA, S7C
'''
# Function to add a student's details
def APPEND():
try:
with open('students.txt','a') as f1:
N=input("Enter student name: ")
RN=input("Enter roll number: ")
f1.write(N+','+RN+','+'\n')
print("Student added successfully!")
except:
print("Append error!")
# Function to display all students and their details
def DISPLAY():
try:
with open("students.txt","r") as f1:
L=f1.readlines()
if len(L)==0:
print("No student records found.")
else:
for i in L:
L1=i.split(',')
N=L1[0]
RN=L1[1]
print("Name:",N,"Roll No.:",RN)
except:
print("No student records found.")
# Function to search for a student by roll number
def SEARCH():
RN=input("Enter roll number to search: ")
F=0
try:
with open("students.txt","r") as f1:
L=f1.readlines()
if len(L)==0:
print("No student records found.")
else:
for i in L:
L1=i.split(',')
if L1[1]==RN:
print("Student Found - Name:",L1[0])
F=1
break
if F==0:
print("Student not found.")
except:
print("No student records found.")
# Function to delete a student by roll number
def DELETE():
RN=input("Enter roll number to delete: ")
F=0
try:
with open("students.txt","r") as f1:
L=f1.readlines()
with open("students.txt","w") as f2:
for i in L:
L1=i.split(',')
if L1[1]!=RN:
f2.write(i)
else:
F=1
if F==1:
print("Student record deleted successfully.")
else:
print("Student not found.")
except:
print("No student records found!")
#Menu Based Mechanism of the program
N=0 #Intialising variable for choice
print("Student Management System")
print("1. Add Student")
print("2. Display all Students")
print("3. Search Student")
print("4. Delete Student")
print("5. Exit")
while N!="5":
N=input("Enter your choice: ")
if N=='1':
APPEND()
elif N=='2':
DISPLAY()
elif N=='3':
SEARCH()
elif N=='4':
DELETE()
elif N=='5':
print("Thank you for using the program!")
break
else:
print("Invalid choice! Please try again.")'''
#_____________________________________________________________________________
#TASK 8, 14/11/2024
'''Take a sample of ten phishing e-mails (or any text file) and find most commonly occurring word(s) '''
#SOHAM SURANA, S7C
'''
def FIND(f):
try:
with open(f,"r") as f1:
C=f1.read()
LW=C.lower().split()
#Remove punctuation
CW=[] #Cleaned Words list
for i in LW:
i=i.strip(".,!?;:'\"()")
CW.append(i)
FT={} # Word Count Dictionary
for i in CW:
if i in FT:
FT[i]+=1
else:
FT[i]=1
#Find the top 5 most common words
print("Most Common Words:")
C1=0
for j in sorted(FT,key=FT.get,reverse=True):
print(j,":",FT[j],"times")
C1+=1
if C1==5: # Stop after 5 words
break
except ZeroDivisionError:
print("File not found!")
# Example usage
FIND("phishing_emails.txt")
'''
#_____________________________________________________________________________
#_____________________________________________________________________________
#PYTHON-BINARY FILE OPERATIONS:
#TASK 1, 14/11/2024
'''CREATING AND ADDING data to a binary file'''
#SOHAM SURANA, S7C
'''
import pickle
def APPEND():
with open("numbers.dat","ab") as f1:
N=[10,20,40,30,60]
L=[N]
pickle.dump(L,f1)
print("Numbers written to binary file.")
APPEND()'''
#_____________________________________________________________________________
#TASK 2, 14/11/2024
'''DISPLAYING DATA FROM BINARY FILE'''
#SOHAM SURANA, S7C
'''import pickle
def DISPLAY():
try:
with open("numbers.dat","rb") as f1:
L= pickle.load(f1)
for i in L:
print(i)
except:
print("File not found.")
DISPLAY()'''
#_____________________________________________________________________________
#TASK 3, 14/11/2024
'''DELETING DATA FROM BINARY FILE'''
#SOHAM SURANA, S7C
'''import pickle
def DELETE():
F=0
try:
with open("numbers.dat","rb") as f:
L=pickle.load(f)
except:
print("File not found!")
# Display current numbers
print("Current Numbers:",L)
# Get the number to delete from the user
N=int(input("Enter the number to delete: "))
L1=[]
for i in L:
if N in i:
i.remove(N)
print("Number deleted successfully!")
L1.append(i)
F=1
else:
L1.append(i)
if F==0:
print("Number not found in the file!")
# Write the updated list back to the file
with open("numbers.dat","wb") as f2:
pickle.dump(L1, f2)
DELETE()'''
#_____________________________________________________________________________
#TASK 4, 14/11/2024
'''Menu based program for a binary file with roll number, name and marks. Input a roll number and
update the marks'''
#SOHAM SURANA, S7C
'''
import pickle
f="student.dat"
# Function to add a new student record
def APPEND():
L=[]
with open(f,"ab") as f1:
N1=int(input("Enter no. of students whose records you would like to enter: "))
for i in range(N1):
RN=int(input("Enter Roll Number: "))
N=input("Enter Name: ")
M=int(input("Enter Marks: "))
S=[RN,N,M]
L.append(S)
pickle.dump(L,f1)
print("Student record(s) added successfully.")
# Function to display all student records
def DISPLAY():
try:
f1=open(f,'rb')
print("Student Records:")
while True:
try:
S=pickle.load(f1)
for i in S:
print("Roll Number:",i[0],",Name:",i[1],",Marks:",i[2])
except:
f1.close()
break
except:
print("No student records found.")
# Function to update marks of a student
def UPDATE():
RN=int(input("Enter Roll Number to update marks: "))
F=0
U=[] #To store updated records
try:
#Read all records and update the specified student's marks
with open(f,"rb") as f1:
while True:
try:
S=pickle.load(f1)
for i in S:
if i[0]==RN:
NM=int(input("Enter new marks: "))
i[2]=NM
F=1
U.append(i)
print("Marks updated successfully.")
else:
U.append(i)
except:
break
# Write the updated records back to the file
with open(f, "wb") as f2:
pickle.dump(U,f2)
if F==0:
print("Student with roll number",RN,"not found.")
except:
print("No student records found.")
#MENU BASED MECHANISM OF THE PROGRAM:
N=0
print("Student Management System")
print("1. Add Student")
print("2. Display Students")
print("3. Update Marks")
print("4. Exit")
while N!="4":
N=input("Enter your choice: ")
if N=="1":
APPEND()
elif N=="2":
DISPLAY()
elif N=="3":
UPDATE()
elif N=="4":
print("Thank you for using the program!")
break
else:
print("Invalid choice! Please try again.")'''
#_____________________________________________________________________________
#_____________________________________________________________________________
#_____________________________________________________________________________
#PYTHON-CSV FILE OPERATIONS:
#TASK 1, 15/11/2024
'''Creating and appending data to a csv file'''
#SOHAM SURANA, S7C
'''
import csv
def CA(S):
try:
with open('test.csv','a',newline='') as f1:
RW=csv.writer(f1)
RW.writerow(S)
print("Data entered successfully!")
except:
print("Append error!")
N=input("Enter data/sentence: ")
L=[N]
CA(L)'''
#_____________________________________________________________________________
#TASK 2, 14/11/2024
'''Displaying data from a csv file'''
#SOHAM SURANA, S7C
'''
import csv
def DISPLAY():
f1=open('test.csv','r',newline='\r\n')
R=csv.reader(f1)
for i in R:
print(i)
f1.close()
DISPLAY()'''
#_____________________________________________________________________________
#TASK 3, 14/11/2024
'''Deleting data from csv'''
#SOHAM SURANA, S7C
'''import csv
def DELETE():
F=0
N=input("Enter data to be deleted: ")
f1=open('test.csv','r',newline='\r\n')
OR=csv.reader(f1)
NR=[]
for i in OR:
if(i[0].lower()==N.lower()):
print('The data has been deleted')
F+=1
else:
NR.append(i)
f1.close()
if F>0:
f2=open('test.csv','w',newline='')
RW=csv.writer(f2)
RW.writerows(NR)
f2.close()
else:
print('No match found!')
DELETE()'''
#_____________________________________________________________________________
#TASK 4, 15/11/2024
'''Create a csv file with name and roll number. Search for a given roll number and display the name, if
not found display
appropriate message using menu based mechanism '''
'''
import csv
f="students.csv"
# Function to add data to the CSV file using a list
def APPEND():
N1=int(input("Enter number of times you would like to enter the names of students: "))
with open(f,"a",newline='') as f1:
RW=csv.writer(f1)
for i in range(N1):
RN=int(input("Enter Roll Number: "))
N=input("Enter Name: ")
S=[RN,N]
RW.writerow(S)
print("Data added to Student records.")
# Function to read and diplay all data from the CSV file
def DISPLAY():
try:
with open(f,"r",newline='\r\n') as f1:
R=csv.reader(f1)
print("Student Records:")
for i in R:
print("Roll Number:",i[0],",Name:",i[1])
except:
print("File not found.")
def SEARCH():
RN1=input("Enter roll number of student whose details you would like to search: ")
F=0
try:
with open(f,"r",newline='\r\n') as f2:
R=csv.reader(f2)
for i in R:
if i[0]==RN1:
print("Record Found-- Name:",i[1])
F=1
break
if F==0:
print("No records with above roll number found!")
except:
print("File not found!")
# Menu based mechanism of the program
print("CSV File Operations Menu")
print("1. Append")
print("2. Display all Data")
print("3. Search by Roll number")
print("4. Exit the program")
N=0 #Initialising variable for choice
while True:
N=input("Enter your choice: ")
if N=="1":
APPEND()
elif N=="2":
DISPLAY()
elif N=="3":
SEARCH()
elif N=="4":
print("Thank you for using the program!")
break
else:
print("Invalid choice! Please try again.")'''
#_____________________________________________________________________________
#TASK 5, 15/11/2024
'''Create a csv file with roll number, name and marks. Input a roll number and update the marks. '''
#SOHAM SURANA, S7C
'''
import csv
f='student.csv'
# Function to create AND append records
def APPEND():
with open(f,'w',newline='') as f1:
RW=csv.writer(f1)
N1=int(input("Enter number of times you would like to enter a stundent's records: "))
for i in range(N1):
RN=input('Enter Roll Number: ')
N=input('Enter Name of student: ')
M=input('Enter Marks: ')
Data=[RN,N,M]
RW.writerow(Data)
print("Data added successfully.")
# Function to display records
def DISPLAY():
try:
with open(f,'r',newline='\r\n') as f2:
R = csv.reader(f2)
for i in R:
print("Roll no:",i[0],"Name:",i[1],"Marks:",i[2])
except:
print("File not found!")
# Function to search for a record
def SEARCH():
F=0
N=input('Enter roll no. of student who has to be searched: ')
try:
with open(f,'r',newline='\r\n') as f2:
R = csv.reader(f2)
for i in R:
if i[0]==N:
print("Roll no:",i[0],"Name:",i[1],"Marks:",i[2])
F+=1
if F==0:
print('No match found!')
else:
print('Total',F,'match(es) found!')
except:
print("File not found!")
# Function to modify marks of a student
def MODIFY():
F=0
N=input('Enter the roll no. of student whose marks are to be modified: ')
try:
with open(f,'r',newline='\r\n') as f2:
OR=csv.reader(f2)
NR=[]
for i in OR:
if i[0]==N:
i[2]=input('Enter the new Marks of student: ')
F+=1
NR.append(i)
if F>0:
with open(f,'w',newline='') as f1:
RW=csv.writer(f1)
RW.writerows(NR)
print("Marks modified successfully.")
else:
print('No match found!')
except:
print("File not found!")
# Function to delete a record
def DELETE():
F=0
N=input('Enter roll no of student whose details are to be deleted: ')
try:
with open(f,'r',newline='\r\n') as f2:
OR=csv.reader(f2)
NR=[]
for i in OR:
if i[0]==N:
print('The record', i[0], ',', i[1],',',i[2], 'has been deleted')
F+=1
else:
NR.append(i)
if F>0:
with open('phone.csv','w',newline='') as f1:
RW=csv.writer(f1)
RW.writerows(NR)
print("Record deleted successfully.")
else:
print('No match found!')
except:
print("File not found!")
# Menu based mechanism of the program
print("Student Management System")
print("1. Create and APPEND Records")
print("2. Display all Records")
print("3. Search for a Record")
print("4. Modify marks of a student")
print("5. Delete a Record")
print("6. Exit")
N=0 #Initialising variable for choice
while N!=6:
N=input("Enter your choice: ")
if N=='1':
APPEND()
elif N=='2':
DISPLAY()
elif N=='3':
SEARCH()
elif N=='4':
MODIFY()
elif N=='5':
DELETE()
elif N=='6':
print("Thank you for using the program!")
break
else:
print("Invalid choice, please try again.")'''
#_____________________________________________________________________________
#_____________________________________________________________________________
#_____________________________________________________________________________