Practical Report File Extended
Practical Report File Extended
#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
#_____________________________________________________________________________
#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()
#_____________________________________________________________________________
#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()
print("Vowels:",VC)
print("Consonants:",CC)
print("Uppercase Characters:",UC)
except:
print("File not found.")
#_____________________________________________________________________________
#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!")
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
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!")
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"
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.")
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!")
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 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!")
#_____________________________________________________________________________
#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'
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!")
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!")