I/O Flashcards
What does a program do when it needs to save data for future use?
writes a data in a file. Normally used .txt files so far.
Name some software that store data
Word processors Image Editors Spreadsheet Games Web browsers
Programmers usually refer to storing data to a file as?
writing data to
How is a piece of data written to a file?
copied from a variable in RAM to the file
output file
used to describe a file that the data is written to
Why is it called an output file?
because the program stores output in it
What is the process of retrieving data from a file known as?
reading
Input file
used to describe a file that is read from
Why is it called an input file?
because the program gets the input from it
What are the three steps that must be taken when a file is used by a program.
open the file
process the file
close the file
Open the file
Opening a file creates connection between the file and the program.
Opening an output file creates a file on the disk and allows the program to write data to it.
Opening an input file allows to read the data from a file
Process a file
In this step data is either written to the file or read from the file
close a file
When the program is finished using the file, it must be closed. Closing a file disconnects the file from a program.
What are the two types of files?
Binary and text
A text file?
encoded as text using scheme : ASCII or unicode.
Viewable with text editors
A binary file
not converted to text. Cannot be opened with text editors
What are the two file access methods?
sequential
direct
What is Sequential access file?
You access data from the beginning of the file to the end of the file. You cant jump around. Old tape. Cannot hop around. If you want to listen to track #5 have to listen to all tracks 1 -4
What is Direct access file?
You can jump directly to any piece of data in the file without having to read the data that comes before it.
LIke MP3 players
Direct access file is also known as?
Random access file
What is a file object?
An object that is associated with a specific file and provides a way for the program to work with the file.
What function do you use to open a file in Phyton?
open
Syntax of writing to a file
FileWrite = open (“filename.txt”, someMode)
Name the modes
r, w and a
Describe the r mode
read only. The file cannot be changed or written to.
Describe the w mode
open the file for write only. If file exists, delete the contents. If file does not exist, re-create it.
Describe ‘a’ mode
Open a file to be written to. If a file exits, add to the end. If not creates it.
test_file = open (r ‘c:\Blake\here.txt’, ‘w’)
What is r? Why is it needed?
raw string.
Otherwise \ will be considered as escape character and give out error.
Syntax to write something to a file
FileWrite.write(string)
Syntax to close
FileWrite.close()
The data that is stored in a file is frequently organized in?
records and fields
What is a record?
A complete set of data about an item
What is a field?
An individual piece of data within a record.
What is an exception?
An exception is an error that occurs when the program is running causing the program to abruptly halt.
How can you handle exceptions?
use try/catch statements
What is ‘traceback”?
It is an error message that gives information regarding the line numbers that caused exception.
try:
(tab)statement
(tab)statement
what is this statement block called?
the try suite.
What is created when an exception is thrown in the memory?
an exception object
How do you display the default error message
except ValueError as sucks:
print(sucks)
the else clause in the try/catch statement. try: statement except someerror: statement else: statement what is the block of statement after else called? explain how this works.
else suite.
try some thing gets executed. If an exception is thrown, goes to the except and something is done. Does not go to else statement…
BUT if not exception is raised, skips the except and goes to else.
Explain how finally clause in try/execute statement works.
finally at the end. try: statement except: statement finally: statement. Now finally executes after try/except and whether or not the exceptions are raised or not raised.
What is the purpose of finally?
For clean up operation. It closes files and other resources
What if an exception is not handled?
It will cause the program to halt.
What type of exception does a program raise when it tries to open an non-existence file?
IO error
What type of exception does a program raise when float tries to convert to non-numeric string to number?
Value Error
How do you handle multiple exceptions?
add as many except: statements