EXERCISE - 7 FILES

A) Write a program to print each line of a file in reverse order. B) Write a program to compute the number of characters, words and lines in a
file. A) AIM: Write a program to print each line of a file in reverse order.
SOURCE CODE:
filename=input("Enter file name:\n") fp1=open(filename,"r") filedata=fp1.read() fp1.close() lines=filedata.split('\n') for line in lines: i=len(line)-1 while(i>=0): print(line[i],end='') i=i-1 print()
OUTPUT:
Enter file name: msg.txt ESIR olleH gnimmargorp nohtyP margorp eliF si sihT

B) AIM: Write a program to compute the number of characters, words and lines
in a file.
SOURCE CODE:
filename=input("Enter file name:\n") fp1=open(filename,"r") filedata=fp1.read() fp1.close() nchars=nwords=nlines=0 for c in filedata: if(c): nchars=nchars+1 if(c==' '): nwords=nwords+1 if(c=='\n'): nwords=nwords+1 nlines=nlines+1 print("Number of characters in a file : ",nchars) print("Number of words in a file : ",nwords) print("Number of lines in a file : ",nlines)
OUTPUT:
Enter file name: msg.txt Number of characters in a file : 51 Number of words in a file : 8 Number of lines in a file : 3