Class 12 : Data File Handling

Class 12 File Handling Questions

File Handling Set 1
File Handling Set 2
File Handling Set 3
File Handling Set 4
File Handling Set 5
File Handling Set 6
File Handling Set 7

Class XII: Python Data File Handling 5

Question:1
Write a python script to read the contents of a file character by character and display them?(Method:1)
Sol:

```def file_read():
f=open("abc.txt")
while True:
if not c:
#print("end of file")
break
print(c,end='')

#function calling

Question:2
Write a python script to read the contents of a file character by character and display them? (Method:2)
Sol:

```def file_read1():
with open("abc.txt") as f:
while True:
if not c:
#print("end of file")
break
print(c,end='')
#function calling

Question:3
write a (python script) function named count_len() to read the contents of the file “abc.txt” and print its length.
Sol:

```def count_len():
le=0
with open("abc.txt") as f:
while True:
if not c:
break
print(c,end='')
le=le+1
print("length of file ",le)
#function calling
count_len()```

Question:4
write a (python script) function named count_lower() to read the contents of the file “story.txt”. Further count and print total lower case alphabets in the file
Sol:

```def count_lower():
lo=0
with open("story.txt") as f:
while True:
if not c:
break
print(c,end='')
if(c>='a' and c<='z'):
lo=lo+1
print("total lower case alphabets ",lo)
function calling
count_lower()```

Question:5
write a (python script) function named count_upper() to read the contents of the file “story.txt”. Further count and print total upper case alphabets in the file.
Sol:

```def count_upper():
lo=0
with open("story.txt") as f:
while True: