Save
...
2.2 Programming Fundamentals
2.2.3 Additional Programming Techniques
Understanding the use of basic file handling operations
Save
Share
Learn
Content
Leaderboard
Share
Learn
Cards (124)
File handling allows programs to persistently store data beyond
runtime
.
True
The
open()
function is used to establish a connection between a program and a file.
What is the purpose of the
r
e
a
d
(
)
read()
re
a
d
(
)
function?
Extract data from file
The
close()
function is used to terminate the connection between a program and a file.
What is one primary purpose of file handling in programming?
Persistent storage
Match the file handling operation with its description:
Open ↔️ Connects the program to a file
Read ↔️ Retrieves data from the file
Write ↔️ Adds data to the file
Close ↔️ Disconnects the program from the file
Data access in file handling allows programs to load and use
stored
information.
True
The
open()
function takes two main parameters: filename and mode.
What happens if you open a file in "w" mode and it already exists?
It is overwritten
An example of opening a file for writing is:
f
i
l
e
=
file =
f
i
l
e
=
o
p
e
n
(
"
d
a
t
a
.
t
x
t
"
,
"
w
"
)
open("data.txt", "w")
o
p
e
n
(
"
d
a
t
a
.
t
x
t
"
,
"
w
"
)
, where the mode is "w".
The
open
operation establishes a connection between the program and a
file
The 'close' operation terminates the connection between the program and the
file
What is the primary purpose of file handling in terms of data storage?
Persistent storage
What is the purpose of the
o
p
e
n
(
)
open()
o
p
e
n
(
)
function in file handling?
Establish a connection
What happens when you open a file with the "w" mode in
o
p
e
n
(
)
open()
o
p
e
n
(
)
?
Creates or overwrites
The
r
e
a
d
(
)
read()
re
a
d
(
)
function can take an optional parameter to specify the number of characters
What does the
r
e
a
d
(
)
read()
re
a
d
(
)
function do if no parameter is provided?
Reads entire file
The
w
r
i
t
e
(
)
write()
w
r
i
t
e
(
)
function takes a single argument, which is the data to be written
What is file handling in programming?
Reading from and writing to files
File handling operations allow programs to store data beyond their
runtime
.
True
Arrange the key file handling operations in the correct sequence.
1️⃣ Open
2️⃣ Read ||| Write
3️⃣ Close
What is the purpose of persistent storage in file handling?
Save data beyond runtime
Why is data exchange a main purpose of file handling?
Facilitates sharing data between programs
What does the "filename" parameter in the open() function specify?
The name of the file
What is the purpose of the read() function?
Extract data from a file
If no size is specified in the read()
function
, the entire file is read.
True
The write() function adds data to a file from the
program
.
The read() function extracts data, while the write() function adds data.
True
Closing a file releases system
resources
used by the file.
A FileNotFoundError occurs when the
program
tries to access a file that does not exist.
True
It is optional to handle errors during file handling operations using try-except blocks.
False
An IOError occurs when there's a general input/output error during file
handling
In the example code, what error is caught if the file "data.txt" does not exist?
FileNotFoundError
What specific error is handled in the last except block of the example code?
IOError
Opening a file in "r" mode allows you to write to it.
False
When reading the entire contents of a file using file.read(), the data is stored in a single
string
What method is used to remove leading and trailing whitespace from a line read from a file?
strip()
What is the purpose of file handling in programming?
Store and retrieve data
The `"\n"` sequence in
Python
is used to create a new line in a file.
True
Opening a file in "w" mode will overwrite the file if it
exists
.
See all 124 cards