0% found this document useful (0 votes)
6 views

Text File - Programs

The document contains a series of Python programs for various file operations, including reading specific bytes, reading entire files, counting lines, and managing student records. It demonstrates how to read and write to files, count characters, vowels, consonants, and manipulate file contents based on specific criteria. Each program is presented with code snippets and comments to guide the user through the process.

Uploaded by

thanoshu55
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Text File - Programs

The document contains a series of Python programs for various file operations, including reading specific bytes, reading entire files, counting lines, and managing student records. It demonstrates how to read and write to files, count characters, vowels, consonants, and manipulate file contents based on specific criteria. Each program is presented with code snippets and comments to guide the user through the process.

Uploaded by

thanoshu55
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

WAP to read 30 bytes from a text file and then read another 30 characters from
the last position read.

1 myfile = open ("<file_name_with_extension>")


2 s1 = myfile.read (30)
3 print (s1)
4 s2 = myfile.read (30)
5 print (s2)
6 myfile.close ()

2. WAP to read a file’s entire content.

1 f = open ("<file_name_with_extension>")
2 s = f.read ()
3 print (s)
4 myfile.close ()

3. WAP to read a file’s first three lines – line by line.

1 f = open ("<file_name_with_extension>", " r")


2 s = f.readline ()
3 print (s, end = " ")
4 s = f.readline ()
5 print (s, end = " ")
6 s = f.readline ()
7 print (s, end = " ")
8 myfile.close ()

4. WAP to read a complete file – line by line.

1 myfile = open (r"<complete_file_path>")


2 s = " "
3 while s :
4 s = myfile.readline ()
5 print (s, end=" ")
6 myfile.close ()
5. WAP to display the size of a file in bytes.

1 myfile = open ("Alphabet.txt", "r")


2 string = myfile.read()
3 size = len (string)
4 print ("Size of file is", size, "bytes")
5 myfile.close()

6. WAP to display the number of lines in a text file.

1 f1 = open ("Alphabet_line.txt")
2 s = f1.readlines()
3 print ("The number of lines is", len(s))
4 f1.close()

7. WAP to obtain the roll number, names and marks of students from the user and
store these in a new file called “Marks.txt” .

1 count = int (input ("How many students ? "))


2 f = open ("Marks.txt", "w")
3 for i in range (count) :
4 print ("Enter details for student", (i+1), "below : ")
5 rollno = int (input ("Rollno : "))
6 name = input ("Name : ")
7 marks = float (input ("Marks : "))
8 rec = str(rollno) + "," + name + "," + str(marks) + '\n'
9 f.write (rec)
10 f.close ()
8. WAP to add more students’ details to the file created in the above program.

1 count = int (input ("How many more students ? "))


2 f = open ("Marks.txt", "a")
3 for i in range (count) :
4 print ("Enter details for student", (i+1), "below : ")
5 rollno = int (input ("Rollno : "))
6 name = input ("Name : ")
7 marks = float (input ("Marks : "))
8 rec = str(rollno) + "," + name + "," + str(marks) + '\n'
9 f.write (rec)
10 f.close ()

9. WAP to display the contents of “Marks.txt” .

1 f = open ("Marks.txt")
2 while str :
3 str = f.readline ()
4 print (str)
5 f.close ()

10. WAP to read a text file line by line and display each word separated by a ‘#’ .

1 file = open ("<file_name_with_extension>")


2 line = " " #initially storing a space (a non-None value)
3 while line :
4 line = myfile.readline() #one line read from file
5 for word in line.split() :
6 print (word, end = '#') #printing each word using split()
7 print()
8 file.close() #close the file
11. WAP to read a text file and count the number of vowels and consonants in the
file.

1 f = open ("Alphabet.txt", "r")


2 x = f.read ()
3 v = c = 0
4
5 for i in x :
6 if i.isalpha () :
7 if i in "AEIUOaeiuo" :
8 v += 1
9 else :
10 c += 1
11
12 print ("No. of vowels =", v)
13 print ("No. of consonants =", c)
14 f.close ()

12. WAP to remove all the lines that contain the character 't' in a file and write it to
another file.

1 file5 = open ("<file_name_with_extension>")


2 lines = file5.readlines()
3 file5.close()
4
5 file5 = open ("<file_name_with_extension>", "w")
6 file5n = open ("<another_file_name_with_extension>", "w")
7
8 for i in lines :
9 if 't' in i :
10 file5n.write (i)
11 else :
12 file5.write (i)
13
14 file5.close()
15 file5n.close()
13. WAP to read a text file and count the number of uppercase and lowercase
letters in the file.

1 f = open ("myfile.txt", "r")


2 x = f.read ()
3 uc = lc = 0
4
5 l = len (x)
6
7 for i in x :
8 if i.isalpha () :
9 if i in "AEIUOaeiuo" :
10 v += 1
11 else :
12 c += 1
13
14 print ("No. of vowels =", v)
15 print ("No. of consonants =", c)
16 f.close ()

You might also like