Unit 4 Python Ques -Ans
Unit 4 Python Ques -Ans
Sample Solution:-
def file_read(fname):
txt = open(fname)
print(txt.read())
file_read('test.txt')
2. Write a Python program to append text to a file and display the text..
Sample Solution:-
def file_read(fname):
myfile.write("Python Exercises\n")
myfile.write("Java Exercises")
txt = open(fname)
print(txt.read())
file_read('abc.txt')
Sample Output:
Python Exercises
Java Exercises
5. Write a Python program to read a file line by line and store it into a list.
Python Code:
def file_read(fname):
with open(fname) as f:
content_list = f.readlines()
print(content_list)
file_read(\'test.txt\')
Flowchart:
6. Write a Python program to read a file line by line store it into a variable.
Sample Solution:-
Python Code:
def file_read(fname):
data=myfile.readlines()
print(data)
file_read('test.txt')
7. Write a Python program to read a file line by line store it into an array.
Sample Solution:-
Python Code:
def file_read(fname):
content_array = []
with open(fname) as f:
for line in f:
content_array.append(line)
print(content_array)
file_read('test.txt')
Sample Output:
print(longest_word('test.txt'))
Flowchart:
Sample Solution:-
Python Code:
def file_lengthy(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
Sample Output:
Sample Solution:-
Python Code:
with open(fname) as f:
return Counter(f.read().split())
Flowchart:
11. Write a Python program to get the file size of a plain file.
Sample Solution:-
Python Code:
def file_size(fname):
import os
statinfo = os.stat(fname)
return statinfo.st_size
Sample Solution:-
Python Code:
for c in color:
myfile.write("%s\n" % c)
content = open('abc.txt')
print(content.read())
Sample Output:
Red
Green
White
Black
Pink
Yellow
Flowchart:
13. Write a Python program to copy the contents of a file to another file .
Sample Solution:-
Python Code:
copyfile('test.py', 'abc.py')
Sample Output:
abc.py
Flowchart:
14. Write a Python program to combine each line from first file with the
corresponding line in second file.
Sample Solution:-
Python Code:
print(line1+line2)
Sample Output:
Red
Welcome to w3resource.com.
Green
Append this text.Append this text.Append this text.
------
Yellow
Append this text.
Flowchart:
Sample Solution:-
Python Code:
import random
def random_line(fname):
lines = open(fname).read().splitlines()
return random.choice(lines)
print(random_line('test.txt'))
Copy
Sample Output:
Sample Solution:-
Python Code:
f = open('abc.txt','r')
print(f.closed)
f.close()
print(f.closed)
Sample Output:
False
True
Flowchart:
17. Write a Python program to remove newline characters from a file.
Sample Solution:
Python Code:
def remove_newlines(fname):
flist = open(fname).readlines()
print(remove_newlines("test.txt"))
Sample Output:
Sample Solution:
Python Code:
def count_words(filepath):
with open(filepath) as f:
data = f.read()
print(count_words("words.txt"))
Sample Output:
33
Flowchart:
19. Write a Python program to extract characters from various text files and puts
them into a list.
Sample Solution:
Python Code
import glob
char_list = []
files_list = glob.glob("*.txt")
char_list.append(f.read())
print(char_list)
Flowchart:
20. Write a Python program to generate 26 text files named A.txt, B.txt, and so
on up to Z.txt.
Sample Solution:
Python Code:
import string, os
if not os.path.exists("letters"):
os.makedirs("letters")
f.writelines(letter)
21. Write a Python program to create a file where all letters of English alphabet
are listed by specified number of letters on each line.
Sample Solution:
Python Code:
import string
def letters_file_line(n):
alphabet = string.ascii_uppercase
f.writelines(letters)
letters_file_line(3)
Copy
Output:
words1.txt
ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX
YZ
Flowchart: