0% found this document useful (0 votes)
5 views6 pages

Unit 4

The document discusses file input/output (I/O) operations in Python, including how to open, read, write, and close files. It details the functions open(), read(), readline(), readlines(), write(), and writelines(), explaining their purposes and return values. The document provides examples for each function to illustrate their usage in handling files.
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)
5 views6 pages

Unit 4

The document discusses file input/output (I/O) operations in Python, including how to open, read, write, and close files. It details the functions open(), read(), readline(), readlines(), write(), and writelines(), explaining their purposes and return values. The document provides examples for each function to illustrate their usage in handling files.
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/ 6

Unit 4 – python

1) Discuss file i/o in python. How to perform


open,read,write and close into a file? (imp)
ANS- File I/O (Input/Output) allows you to read data from
files and write data to files. This is crucial when dealing
with persistent data storage, like saving user input, logging
information, or reading data for further processing.
Python provides a built-in function open() to work with
files. The common operations associated with file
handling are:
1) Open() – function is used to open a file
It takes two parameters file name and mode

Open(filename,mode) – opening a file

‘r’ – to read(default mode)


‘w’- to write
‘a’ – append, creates a new file if doesn’t exist
‘b’- binary modes like ‘rb’, ‘wb’
‘r+’- read and write mode
‘w+’- write and read mode
Example-
file = open("example.txt", "w") # Open for writing
(creates a new file)
2) Reading from a file –
• Read()- Reads the entire content of the file as a
single string.
• readline(): Reads one line from the file at a time.
• readlines(): Reads all lines and returns them as
a list of string
example-
file= open(“sample.txt”, “r”)
content = file.read()
print(content)

3) writing to a file – ( to write a file you must open


file in w or a )
write()- writes a string to file
writelines()- writes a string to file
example-
file = open(“sample.txt”,”w”)
file.write(“hellow world”)

4) closing a file – close()


file.close() – is used in the end
(100 %) ques 2) write purpose of
read()
readline()
readlines()
write()
writelines()
and what does it return or how does it operates?
Ans)
read()
Purpose:
• The read() function reads the entire content of the file and
returns it as a single string.
• When called, it reads all the data from the file from the current
file pointer position to the end of the file.
• The file pointer moves to the end of the file after the operation.
• If the file is large, you can limit how many characters to read by
passing an argument (e.g., read(10) will read the first 10
characters).
• Returns a string containing the content of the file.
• If the end of the file is reached, it returns an empty string ('').

Example –
with open("example.txt", "r") as file:
content = file.read()
print(content) # Prints entire content of the file
readline()-
The readline() function reads the next line from the file.
Each call to readline() reads one line from the file.
After reading, the file pointer is moved to the beginning of
the next line.
It reads until it encounters a newline character (\n), which
signifies the end of a line.
If the file is already at the end, it returns an empty string.
Returns a string containing a single line from the file.
Example –
with open("example.txt", "r") as file:
line = file.readline()
print(line) # Prints one line from the file
Readlines() –
The readlines() function reads all lines from the file and
returns them as a list of strings.
It reads the entire content of the file, splitting it into lines.
Each line is returned as a string within a list.
The newline characters (\n) at the end of each line are
included in the string.
Returns a list of strings, where each string represents one
line of the file.
Write() -
• The write() function is used to write a string to a file.
• It writes the content of the string passed as an argument to the
file.
• It does not append a newline (\n) at the end of the string; you
must explicitly include a newline if needed.
• The file pointer is moved to the end of the string after writing.

Returns the number of characters written to the file (not the


string itself).

Example-
with open("example.txt", "w") as file:
file.write("This is a line in the file.\n")
file.write("This is another line.")
writelines() –
The writelines() function is used to write a list of strings to a file.
It takes an iterable (like a list or tuple) containing strings and
writes each string to the file.
Returns None (it does not return any value, but writes the strings
to the file).

Example –
lines = ["This is the first line.\n", "This is the second line.\n",
"This is the third line.\n"]
with open("example.txt", "w") as file:
file.writelines(lines)

You might also like