Wednesday, November 20, 2024

FILE HANDLING TASKS

1. Write a Python program to read an entire text file.

#Code :

print("READING THE ENTIRE FILE")

file=open("sam.txt",'r')

print(file.read())

file.close()


2. Write a Python program to read the first n lines of a file.
#Code :

print("READING N LINES")

file=open("sam.txt",'r')

n=int(input("Enter the No.of.Lines : "))

for i in range(n):

    print(file.readline())

file.close()


3. Write a Python program to append text to a file and display the text.
#Code :

print("APPENDING THE TEXT TO THE EXISTING FILE")

file=open("sam.txt",'a')

line=input("Enter the Lines :\n")

file.write(line)

file.close()

file=open("sam.txt",'r')

print(f"\n{file.read()}")

file.close()


4. Write a Python program to read the last n lines of a file.
#Code :

print("READING THE LAST N LINES")

file=open("sam.txt",'r')

n=int(input("Enter the No.of.Lines : "))

lines = file.readlines()[-n:]

for i in lines:

    print(i.strip())

file.close()


5. Write a Python program to read a file line by line and store it into a list.
#Code :

file=open("sam.txt",'r')

lines = file.readlines()

line_list=list()

for i in lines:

    line_list.append(i.strip())

for i in line_list:

    print(i)

file.close()


6. Write a Python program to read a file line by line and store it into a variable.
#Code :

file=open("sam.txt",'r')

line=file.readlines()

lines=""

for i in line:

    lines=lines+"\n"+i.strip()

print(lines)

file.close()


7. Write a Python program to read a file line by line and store it into an array.
#Code :

import array as a

file=open("sam.txt",'r')

arr=a.array('u')

lines=file.readlines()

line=""

for i in lines:

    line=line+"\n"+i.strip()

arr=line

print(arr)

file.close()


8. Write a python program to find the longest words.
#Code :

file=open("file.txt",'r')

lines=file.readlines()

line,word="",""

len_list=[]

for i in lines:

    line=line+i.strip()

words=line.split()

print("******************************")

for i in words:

    word=i.replace("," , "").replace("'","").replace("."," ")

for i in words:

    len_list.append(len(i))

print(len_list)

max_len=max(len_list)

print(max_len)

print(len_list.index(max_len))

print(words[len_list.index(max_len)])

file.close()


9. Write a Python program to count the number of lines in a text file.
#Code :

file=open("sam.txt",'r')

lines=file.readlines()

n=0

for i in lines:

    if i.strip():

        n+=1

print(f"No.of.lines : {n}")

file.close()

 

10. Write a Python program to count the frequency of words in a file.
#Code :

file=open("sam.txt",'r')

lines=file.readlines()

try:

    line,sen="",""

    word=input("Enter the Word : ")

    for i in lines:

        line=line+i.strip()

    words=line.split()

    for i in words:

        sen=sen+" "+i.replace(",","")

    cnt=sen.count(word)

    print(f"The Frequency of {word} is {cnt}")

    file.close()

except:

    print("ERROR FOUND")

    file.close()


11. Write a Python program to get the file size of a plain file.
#Code :

import os.path as os

size=os.getsize("sam.txt")

print(f"The Size of the sam.txt file is {size}")


12. Write a Python program to write a list to a file.
#Code :

file=open("file.txt",'a')

l=["Rolls ","Royce ","Spirit ","Ecstacy "]

for i in l:

    file.write(i)

file=open("file.txt",'r')

print(file.read())

file.close()


13. Write a Python program to copy the contents of a file to another file.
#Code :

f1=open("sam.txt",'r')

f2=open("file.txt",'r')

content=""

lines=f1.readlines()

print("Content of File 1\n")

print(f1.read())

print("************************************************************")

print("Content of File 2\n")

print(f2.read())

print("************************************************************")

for i in lines:

    content=content+i

print(content)

print("************************************************************")

f2=open("file.txt",'a')

f2.write(content)

f2=open("file.txt",'r')

print("Content of File 2 of Coping from File 1\n\n")

print(f2.read())

print("************************************************************")

f1.close()

f2.close()


14. Write a Python program to combine each line from the first file with the corresponding line in the second file.
#Code :

f1=open("sam.txt",'r')

f2=open("file.txt",'r')

f3=open("sample.txt",'a')

limit=int(input("Enter the No.of.Lines : "))

for i in range(limit):

    txt=f1.readline()

    f3.write(txt)

    txt=f2.readline()

    f3.write(txt)

f3=open("sample.txt",'r')

print(f3.read())

f1.close()

f2.close()

f3.close()


15. Write a Python program to read a random line from a file.
#Code :

import random as r

print("RANDOM LINE PRINTING")

file=open("sam.txt",'r')

lines=file.read().splitlines()

rand=r.choice(lines)

print(rand)

file.close()


16. Write a Python program to assess if a file is closed or not.
#Code :

file=open("sam.txt",'r')

if file.close==True:

    print("File Closed")

else:

    print("\nFile Opened\nClosing Process Initiated")

    file.close()


17. Write a Python program to remove newline characters from a file.
#Code :

file=open("sample.txt",'r')

lines=file.readlines()

line=""

print(f"Before Striping\n{lines}")

for i in lines:

    line=line+" "+i.strip()

print(f"After Striping\n{line}")

file.close()


18. Write a Python program that takes a text file as input and returns the number of words of a given text file.
Note: Some words can be separated by a comma with no space.
#Code :

file=open("sam.txt",'r')

lines=file.readlines()

try:

    line,sen="",""

    count=0

    for i in lines:

        line=line+i.strip()

    words=line.split()

    for i in words:

        sen=sen+" "+i.replace(",","")

    print(sen)

    for i in sen:

        if i==" ":

           count+=1

    print(f"The Count of Words is {count}")

    file.close()

except:

    print("ERROR FOUND")


19. Write a Python program to extract characters from various text files and put them into a list.
#Code :

file_chr=[]

f1=open("sam.txt",'r')

f2=open("file.txt",'r')

num=int(input("Enter the No.of.Character : "))

l1=f1.read(num)

l2=f2.read(num)

for i in l1:

    file_chr.append(i)

for i in l2:

    file_chr.append(i)

print(file_chr)

f1.close()

f2.close()


20. Write a Python program to generate 26 text files named A.txt, B.txt, and so on up to Z.txt.
#Code :

ex=input("Enter the File Extension : ").lower()

for i in range(65,91):

    f=open(f"{chr(i)}.{ex}",'w')

    f.close()


21. Write a Python program to create a file where all letters of the English alphabet are listed by specified number of letters on each line.
#Code :

f=open("sample.txt",'r')

c=0

j=1

count=[]

num=int(input("Enter the No.of.Lines : "))

char=input("Enter the Character to be count : ")

for i in range(num):

    lines=f.readline()

    for j in lines:

        if j!='.':

            if j==char:

                c+=1

        else:

            count.append(c)

            c=0

print(f"The Character {char} in the First {num} lines are,")

for i in count:

    print(i)

f.close()


USD TO INR C++ PROGRAMMING

  NALLAMUTHU GOUNDER MAHALINGAM COLLEGE                                                 POLLACHI-642001                                     ...