Skip to main content

File Handling Theory Questions

Computer Science 

( Class XII Sc ) 

( Subject Code 083)








Q1. Difference between r+ and rb+ mode
Both modes open file in reading and writing mode and the  file pointer is placed at the beginning of file. But rb+ is used for binary files

Q2. Write statements for following purpose
(a) Open file abc.txt in read mode      f1=open("abc.txt",r)
(b) Open file abc.txt in write mode      f1=open("abc.txt",w)

Q3. rename file test1 to test2

import os
# Rename a file from test1.txt to test2.txt
os.rename( "test1.txt", "test2.txt" )


Q4.Write a statement in python so that new contents can be added in the end of text file abc.txt

f1=opne("abc.txt",a) # Open file in append mode

Q5. What is file object and file modes?
File object is a reference of file available on Disk.
f1=open("abc.txt",r)

f1 is File object reference the file abc.txt . This is used  to perform operations on file. r mode is used for reading file.
  1.  r “, for reading.
  2.  w “, for writing.
  3.  a “, for appending.
  4.  r+ “, for both reading and writing
  5. rb,wb,ab and rb+ work for binary files as above
Q6. Write a single loop to display all the contents of a file abc.txt without leading and trailing white spaces.

for line in open("abc.txt",r):
             print(line.strip())

Q7.What is an Absolute Path?

An absolute path is a path that describes the location of a file or folder. It is relative to the root directory.

e,g  C:/users/admin/docs/stuff.txt

Q8.What is a Relative Path?

A relative path is a path that describes the location of a file or folder in relative to the current working directory.
if current working directory is  C:/users/admin/, then the relative path to stuff.txt would be: docs/stuff.txt

if current working directory is  C:/users/admin/docs, then the relative path to stuff.txt would be: stuff.txt

Absolute path is a path-name which start from top most level of directory structure while relative path is related to current working directory.
In os.path module two functions abspath() and relpath() function is used to show the absolute and relative path of any file. Here is example to show this.
>>> import os
 >>> os.path.abspath('example1.txt')
>>>os.path.relpath('C:\\Users\\Dell\\AppData\\Local\\Programs\\Python\\P ython37-32\\Programs', 'example1.txt')

Q9.What is the difference between Text File and Binary File ?
Text file store information in form of UNICODE or ASCII code characters.Each Line of text is terminated with special character called EOL.(\n : new line character). It can store only plain text. This is human readable format. You can read text files by simple text editor

Binary file store information in form of bytes. It can store text as well as multimedia contents audio,video or image data. Binary files are faster and easier to read and write because no translation required for new line character. 
Q10. How are following codes different from one another ?

(a)  file=open("abc.txt","r")
       file,read()
(b)  file=open("abc.txt","r")
       file,read(100)
(a) read all contents of  the file abc   (b) read  first 100 bytes of the file abc

Q11. What is the difference between read() and readlines() ?

read() reads from a file in read mode and stores its contents in a string type variable

f=open("abc.txt","r")
s=f.read()
print(s)

output: 
mukesh kumar modi
JNV Chandigarh

readlines() reads from a file in read mode and returns a list of lines 

f=open("abc.txt","r")
list=f.readlines()
print(list)

output :
['mukesh kumar modi\n', 'JNV Chandigarh\n']

Q12. What is the use of tell() method ?

The tell() method returns the current file position in a file stream for read or write operation in number of bytes.

You can change the current file position with the seek() method.
f=open("xyz.txt")
data=f.read(10) # read first 5 bytes
print(data)
print(f.tell())  # returns 10
data=f.read(10) # read next 10 bytes
print(data)
print(f.tell())  # returns 20
data=f.read(15) # read next 15 bytes
print(data)
print(f.tell()) # returns 35
data=f.read(15) # read next 15 bytes
print(data)
f.close()
Q12. What is the use of seek() method ?
The seek() method is used to move  the position of file pointer or cursor in the file for read write operation.
syntax - seek(offset, seek-direction)
Offset: Number of postions to move forward/backword
seek direction value may be 0,1 or 2
0 for beginning, 1 for current position and 2 for end of file position
Its default value is 0.

f.seek(0,0) : move the pointer at beginning of file
Seek() function with negative offset only works when file is opened in binary mode. such as

file.seek(-20,2) # 20 byte backward from the end position
file.seek(-5,1) # backward 5 byte from current position
file.seek(10,0) # forward 10 byte from beginning
file.seek(2) # 2 byte forward from beginning

Comments

Popular posts from this blog

Revision basics of Python covered in Class XI

Python Function Basics