0% found this document useful (0 votes)
4 views2 pages

Files Q&a

Chapter 5 discusses file handling in Python, explaining data files, the differences between text and binary files, and the significance of file objects. It covers methods for reading and writing data, various file opening modes, and the importance of closing files. Additionally, it provides sample Python code for writing to a file and determining file size, line count, and word count.

Uploaded by

menakavision683
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)
4 views2 pages

Files Q&a

Chapter 5 discusses file handling in Python, explaining data files, the differences between text and binary files, and the significance of file objects. It covers methods for reading and writing data, various file opening modes, and the importance of closing files. Additionally, it provides sample Python code for writing to a file and determining file size, line count, and word count.

Uploaded by

menakavision683
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/ 2

***

Chapter-5 File Handling

Q.1 What is a data file in python?

Ans: A bunch of bytes / data stores on some storage device referred by the filename.

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

Ans: A text file stores data as ASCII/UNICODE characters where as a binary file stores data in
binary format (as it is stored in memory). Internal conversion is required in text file and hence
slower but binary file does not need any translation and faster.

Q.3 What is the significance of a file object or a file handle?

Ans: A file object or file handle establishes a link or a reference to a file

Q.4 What is the use of flush( )?

Ans: Forcefully write data from buffer to file.

Q.5 Name the methods used for reading and writing data from text file.

Ans: read( ), readline( ), readlines( ) for reading data from file and write( ), writelines( ) to write
data to a file

Q.6 What are the various file opening modes?

r – read only, w – write only, a – append

rb – read in binary mode, w – write in binary mode, a – append in binary mode

r+ , rb+ : read and write, w+, wb+ , a+ , ab+ : write/append and read

Q.7 What is the significance of close( )?

Ans: When a file is closed data from buffer gets written onto the file on the disk and link from file
is removed. It ensures that data is saved in the file.

Q8. Write a python code to write some text to a file Lines.txt.

Ans: # writing data to file


f=open(‘Lines.txt’,’w’)
s=input(‘Enter text ‘)
f.write(s)
f.close( )

12 | P a g e
Q.9 Write a python code to find the size of the file in bytes, number of lines and number of
words.

# reading data from a file and find size, lines, words


f=open(‘Lines.txt’,’r’)
str=f.read( )
size=len(str)
print(‘size of file n bytes’,size)
f.seek(0)
L=f.readlines( )
word=L.split( )
print(‘Number of lines ’,len(L))
print(‘Number of words ’,len(word))
f.close( )

Q.10. What does stdin, stdout represent?

Ans : stdin represent standard input device and stdout represent standard output device which
are represented as files.

***

13 | P a g e

You might also like