#lines starting with F f=open(r"C:\Users\hp\Desktop\cs\networking\firewall.txt") c=0 for i in f.readline(): if(i=='F'): c=c+1 print(c) ------------------------------------------------------------------ #find no of lines f=open(r"C:\Users\hp\Desktop\cs\networking\firewall.txt") t=f.readlines() print(len(t)) ----------------------------------------------------------------------- #count no of digits f=open(r"C:\Users\hp\Desktop\cs\networking\firewall.txt") t=f.read() c=0 for i in t: if(i>='0' and i<='9') : c=c+1 print(c) ----------------------------------------------------- #count no of alphabets f=open(r"C:\Users\hp\Desktop\cs\networking\firewall.txt") t=f.read() c=0 for i in t: if(i>='a' and i<='z')or(i>='A' and i<='Z') : c=c+1 print(c) -------------------------------------------------------------------- #find size of file or how many characters in a file f=open(r"C:\Users\hp\Desktop\cs\networking\firewall.txt") t=f.read() print(len(t)) --------------------------------------------------------------------------- #find how many 'f' and 's' present in a file f=open(r"C:\Users\hp\Desktop\cs\networking\firewall.txt") t=f.read() c=0 d=0 for i in t: if(i=='f'): c=c+1 elif(i=='s'): d=d+1 print(c,d) -------------------------------------------------------- #find how many 'firewall' and 'to' present in a file f=open(r"C:\Users\hp\Desktop\cs\networking\firewall.txt") t=f.read() c=0 d=0 for i in t.split(): if(i=='firewall'): c=c+1 elif(i=='to'): d=d+1 print(c,d) --------------------------------------------------------------------------- #find how many 'firewall' or 'to' present in a file f=open(r"C:\Users\hp\Desktop\cs\networking\firewall.txt") t=f.read() c=0 for i in t.split(): if(i=='firewall')or (i=='is'): c=c+1 print(c) --------------------------------------------------------------------------- #print 5 lines from file f=open(r"C:\Users\hp\Desktop\cs\networking\firewall.txt") print(f.readlines(5)) ------------------------------------------------------------------------------ f=open(r"C:\Users\hp\Desktop\cs\networking\firewall.txt") print(f.readline())#display single line print(f.readline(3))#display first 3 characters from a line ---------------------------------------------------------------------------------- #display first 3 lines f=open(r"C:\Users\hp\Desktop\cs\networking\firewall.txt") print(f.readline()) print(f.readline()) print(f.readline()) or f=open(r"C:\Users\hp\Desktop\cs\networking\firewall.txt") for i in range(3): print(f.readline()) ------------------------------------------------------------------------------ f=open(r"C:\Users\hp\Desktop\cs\networking\firewall.txt") print(f.read(20))#0 to 20 bytes print(f.read(30))#next 30 bytes i.e. 21 to 30 (upto 50 bytes) ------------------------------------------------------ '''Write a program that reads character from the keyboard one by one. All lower case characters get store inside the file LOWER, all upper case characters get stored inside the file UPPER and all other characters get stored inside OTHERS. ''' f=open("hello.txt") f1=open("lower.txt","a") f2=open("upper.txt","a") f3=open("others.txt","a") r=f.read() for i in r: if(i>='a' and i<='z'): f1.write(i) elif(i>='A' and i<='Z'): f2.write(i) else: f3.write(i) f.close() f1.close() f2.close() f3.close()