[go: up one dir, main page]

0% found this document useful (0 votes)
22 views3 pages

File Handling

The document contains Python programming solutions for file handling tasks, including reading files, creating binary files, and processing text files. It also lists important questions related to file-processing modes, differences between functions, and advantages of data formats. Additionally, it provides various programming exercises to manipulate and analyze file contents.

Uploaded by

connect2pihu09
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)
22 views3 pages

File Handling

The document contains Python programming solutions for file handling tasks, including reading files, creating binary files, and processing text files. It also lists important questions related to file-processing modes, differences between functions, and advantages of data formats. Additionally, it provides various programming exercises to manipulate and analyze file contents.

Uploaded by

connect2pihu09
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/ 3

Q.

Write a program that prompt the user for a file name and
then read and prints the contents of the requested file in the
upper case.

Solution
---------------------------------------------------
file = open ( input("Enter the file name :-")+".txt","r+")
text = file.readlines()
for i in text:
for j in (i):
print (j.upper(), end="")
file.close()

Q. Write a Python program to create a binary file Items.


dat to store Item details of some items as per
{itemno, name, price, category}.

solution:
------------------------------------------------
import pickle
file = open("items.dat","wb")
while True :
itemno = int(input("Enter item Number (for quit enter -1 ):- "))
if itemno == -1 :
break
name = input("Enter Name of item :- ")
pri = float(input ("Enter price of item :-"))
cat = input("Enter Category of item :- ")
lst = [ itemno, name, pri, cat ]
pickle.dump( lst , file )
print()
print()
print("File created successfully !!")
file.close()

important questions:
---------------------------------------------------
Q. What are the different file-processing modes supported by Python?

Q. What is the difference between readline() and readlines() function?

Q. What is the difference between "w" and "a" modes?

Q. Differentiate between file modes r+ and w+ with respect to python.

Q. Differentiate between file modes r+ and rb+ with respect to Python.

Q. What would be the data type of variable data in the following


statements?
(a) data = f.read()
(b) data = f.read(10)
(c) data = f.readline()
(d) data = f.readlines()

Q. What are the advantages of saving data in:

(i) Binary form


(ii) Text form

Q. What is a CSV file?

Q. What are the advantages of CSV file formats?

Q. Differentiate between a text file and a binary file.

Q. Write a method in Python to read lines from a text file MYNOTES.TXT


and display those lines which start with the alphabet 'K'.

Q. What are the advantages of saving data in:

(i) Binary form


(ii) Text form

Q. Write a function file_long() that accepts a filename and reports the


file's longest line.

Q. Write a program to count the number of uppercase alphabets present in


a text file "Pome.txt".

Q. Write a function remove_lowercase() that accepts two file names, and


copies all lines that do not start with a lowercase letter from the
first file to the second file.

Q. Write a program that reads characters from the keyboard one by one.
All lower case characters get stored inside the file LOWER, all upper
case characters get stored inside the file UPPER and all other characters
get stored inside file OTHERS.

Q. Reading a file line by line from the beginning What if you want to
read a file backward? This happens when you need to read log files. Write
a program to read and display content of file from end to beginning.
Q. Write a statement in Python to perform the following operations:

• To open a text file "MYPET.TXT" in write mode


• To open a text file "MYPET.TXT" in read mode

Q. Write a method in Python to write multiple lines of text contents into


a text file daynote.txt line.

Q. Write a Python program to display the size of a file after removing


EOL characters, leading and trailing white spaces and blank lines.

Q. Write a user-defined function in Python that displays the number of


lines starting with 'H' in the file Para.

txt. Example, if the file contains:


Whose woods these are I think I know.
His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow.
Then the line count should be 2.

You might also like