0% found this document useful (0 votes)
19 views4 pages

Filhandlng 4

The document discusses file handling in Python. It explains what a file is, file handling, the open() function syntax, how Python will create a file if it doesn't exist when opening in write mode, and provides an example of creating a data.txt file with sample text.

Uploaded by

vikash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

Filhandlng 4

The document discusses file handling in Python. It explains what a file is, file handling, the open() function syntax, how Python will create a file if it doesn't exist when opening in write mode, and provides an example of creating a data.txt file with sample text.

Uploaded by

vikash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

8.

To read 4th line from text file, which of


the following statement is true?

a. dt = f.readlines();print(dt[3])
b. dt=f.read(4) ;print(dt[3])
c. dt=f.readline(4);print(dt[3])
d. All of these

9 Which of the following function flushes


the files implicitly?

a.flush()
b. close()
c.open()
d. fflush()

10. Which of the following functions


flushes the data before closing the file?

a. flush()
b. close()
c. open()
d. fflush()
1. What do you mean by file? What do
you mean by file handling?
o The file refers to the collection of

bytes stored in computer storage.


o Data can be stored in various forms

in a file.
o These files saved in a specific

format with a specific extension.


o Every file needs to have a specific

program to read them.


o Fille handling refers to the process

of handling data using software for


IO operations.
2. Explain open() function with its
syntax in detail.
o The open function has the following

syntax:
o Open a text file: Syntax:<file

object> =
open(file_name,access_mode)
 file object : It is just like a

variable or object
 open(): It is a function with two
parameters. 
 file_name: It accepts a file name

with .txt extension.


 access_mode: It specifies the

mode to access the file. The


default mode is reading mode.
 These modes are 

 r: to read a file        

 w: to write          

 a: append contents

3. Does python create itself if the


file doesn’t exist in the memory?
Illustrate your answer with an
example.
o Python will create a file

automatically when the open


function is used with write mode.
o Example:

 f=open(“data.txt”,”w”)

 f.write(“Hello\nHow are you?”)

 f.close()
4. Write a statement to create a
data.txt file with the following text.

1. Python file handling is very


interesting and useful.
2. This is a text file created through
python.
 f=open(“data.txt”,”w”)

 f.write(“Python file handling is

very interesting and useful.”)


 f.write(“This is a text file

created through python.”)


 f.close()

You might also like