Deck 8: Text Files

Full screen (f)
exit full mode
Question
Text file is a file containing only printable characters.
Use Space or
up arrow
down arrow
to flip the card.
Question
The newline character causes a new line of text to begin when displayed on the screen.
Question
A text file can be directly displayed on the screen.
Question
Binary files are structured as separate lines as with text file, but can contain numerical data and therefore cannot be directly displayed on the screen.
Question
Which of the following is not true of text files.

A) text files contain printable (displayable) characters
B) text files contain nonprinting characters
C) text files are structured as lines of text
D) text files can be directly displayed on the screen
E) text files cannot contain numeric characters
Question
A ___________________ is a file containing characters structured as lines of text, as well as nonprinting characters such as \n, the newline character.
Question
A ___________________ is a file containing various types of data, such as numerical data, and is not structured as lines of text.
Question
All files must first be opened before they can be read from or written to.
Question
in Python, files are accessed through method calls to an object associated with the open file.
Question
To open a file in Python for reading, the openRead function is called and passed the name of the file to open.
Question
When a program opens a file in Python, the file must reside in the same directory (folder) as the program in order to be found.
Question
A file may be closed and then reopened, with reading starting at the beginning of the file.
Question
To open a file for writing in Python, 'w' is used to indicate that any existing file of the same name should be written over, and 'a' is used to indicate that the output should be appended to an existing file.
Question
It is more likely that an error may occur when opening a file for writing than when opening a file for reading.
Question
Although files should be closed after being opened for either reading or writing. it is especially important to close a file that is open for writing.
Question
When reading from a text file, the readline method is used, which returns all the characters on the current line of the file, up to but not including the newline character.
Question
When the end-of-file is reached when reading from a text file, an error is generated.
Question
When writing a line of text to a text file, the write method does not add a newline character to the output string.
Question
Fundamental operations of files in include opening a file, _______ a file, __________ a file, and___________ a file.
Question
To open a file for reading, the built-in _________ method is used, called with an optional second parameter of ____.
Question
To open a file for writing, the built-in _________ method is used, called with a required second parameter of ____.
Question
When opening a file for reading, an error that may occur is ______________________________.
Question
When reading a line from a text file, the ________________ method is used.
Question
24. When reading a line from a text file, all the characters up to and including the ___________ character are read and returned.
Question
The ___________ method is used to write to a text file.
Question
When writing to or reading from to a file, data is placed in an area of memory called a __________, which make the reading and writing of files more efficient.
Question
Provide Python code that opens a file named names.txt, in which each line of the file contains a friends name in the form firstname lastname, and displays each of the names on the screen, one per screen line.
Question
Provide Python code that prompts the user for the file name of the form filename.txt of a text file, opens the file, and displays each of the names on the screen, one per screen line.
Question
Provide Python code that prompts the user for the file name of the form filename.txt of a text file, opens the file, and writes the contents of the file to a new file name filename-2.txt.
Question
Any of the sequence methods of Python may be applied to strings.
Question
Strings are immutable in Python, and therefore cannot be altered.
Question
Any string method involving the modification of a string returns a new string containing the modification, with the original string the method is called on unchanged.
Question
Collectively, the operations performed on strings is called _______________________.
Question
The ________ string method in Python can be used to determine if a string contains only letters, whereas the __________ method can be used determine is a string contains only digits.
Question
The ________ and __________ string methods in Python can be used to determine if a string contains only lowercase or uppercase characters, whereas the __________ and ___________ methods can be used to convert a string to all lowercase or all uppercase characters.
Question
Give Python code that displays the longer of two names, name1 and name2.
Question
Give Python code that display "Valid SSN" if the string assigned to variable SSN is a valid social security number, and displays "Invalid SSN" otherwise. A social security number is valid if it contains exactly nine digits, and no other characters.
Question
Give Python code that removes any occurrences of dashes from a given social security number that variable SSN is assigned to, and updates SSN to contain the dash-removed result.
Question
Give Python code that, for any state of the U.S. assigned to variable state_name, displays the name ensuring that the first letter is displayed capitalized.
Question
Give Python code that, for a first name and last name assigned to variable full_name with the first and last name separated by a single space (e.g., Alice Morgan), displays the first name only.
Question
Give Python code that for a first name and last name assigned to variable full_name with the first and last name separated by a single space (e.g., Alice Morgan), assigns the first name to variable first_name and the last name to variable last_name.
Question
An exception is an object that is raised when an unexpected situation has occurred.
Question
All raised exceptions should be able to be caught and handled to keep a program executing.
Question
Python contains a predefined set of exceptions called standard exceptions.
Question
Exceptions that are thrown by a given function must be caught by the function to gracefully handle errors.
Question
Exceptions must be caught and handled by the calling code of the function throwing the exceptions.
Question
If an exception is thrown all the back to the main module and not handled there, the program terminates.
Question
The section of code making a call to any function that raises an exception must be contained in a try suite (block).
Question
An exception handler is the section of code that is executed when a particular exception type is caught.
Question
Which of the following is not a standard exception in Python?

A) ImportError
B) IndexError
C) AssignmentError
D) TypeError
E) ValueError
Question
An exception is a value (object) that is __________ when an exceptional situation occurs.
Question
The predefined exceptions in Python are called the ____________ exceptions.
Question
The expression 'Hello' + 10 would raise a _________________ exception on Python.
Question
The expression int('10A') would raise a ________________ exception on Python.
Question
55. The section of code that is to be executed when a particular exception has been caught is called an _____________________________.
Question
Give section of code that prompts the user for an integer value, and displays the number entered if no exception is raised, otherwise catches the exception when a non-numeric character is entered, displaying the message "non-digit found in input."
Question
Write a function called getNum(start, end) that is passed the starting and ending values of a range of integers, prompts the user to enter an integer in the specified range, and returns the integer entered. The function should throw a ValueError exception if the entered number is not within the required range.
Question
Write a function called getYearOfBirth that prompts the user to enter their year of birth as a four-digit number, and returns the year entered. The function should throw a ValueError exception if the entered number is not within the range 1900 to the present year. Write a program that call this function, and keeps calling it until a valid year has been entered.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/58
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 8: Text Files
1
Text file is a file containing only printable characters.
False
2
The newline character causes a new line of text to begin when displayed on the screen.
True
3
A text file can be directly displayed on the screen.
True
4
Binary files are structured as separate lines as with text file, but can contain numerical data and therefore cannot be directly displayed on the screen.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
5
Which of the following is not true of text files.

A) text files contain printable (displayable) characters
B) text files contain nonprinting characters
C) text files are structured as lines of text
D) text files can be directly displayed on the screen
E) text files cannot contain numeric characters
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
6
A ___________________ is a file containing characters structured as lines of text, as well as nonprinting characters such as \n, the newline character.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
7
A ___________________ is a file containing various types of data, such as numerical data, and is not structured as lines of text.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
8
All files must first be opened before they can be read from or written to.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
9
in Python, files are accessed through method calls to an object associated with the open file.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
10
To open a file in Python for reading, the openRead function is called and passed the name of the file to open.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
11
When a program opens a file in Python, the file must reside in the same directory (folder) as the program in order to be found.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
12
A file may be closed and then reopened, with reading starting at the beginning of the file.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
13
To open a file for writing in Python, 'w' is used to indicate that any existing file of the same name should be written over, and 'a' is used to indicate that the output should be appended to an existing file.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
14
It is more likely that an error may occur when opening a file for writing than when opening a file for reading.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
15
Although files should be closed after being opened for either reading or writing. it is especially important to close a file that is open for writing.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
16
When reading from a text file, the readline method is used, which returns all the characters on the current line of the file, up to but not including the newline character.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
17
When the end-of-file is reached when reading from a text file, an error is generated.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
18
When writing a line of text to a text file, the write method does not add a newline character to the output string.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
19
Fundamental operations of files in include opening a file, _______ a file, __________ a file, and___________ a file.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
20
To open a file for reading, the built-in _________ method is used, called with an optional second parameter of ____.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
21
To open a file for writing, the built-in _________ method is used, called with a required second parameter of ____.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
22
When opening a file for reading, an error that may occur is ______________________________.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
23
When reading a line from a text file, the ________________ method is used.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
24
24. When reading a line from a text file, all the characters up to and including the ___________ character are read and returned.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
25
The ___________ method is used to write to a text file.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
26
When writing to or reading from to a file, data is placed in an area of memory called a __________, which make the reading and writing of files more efficient.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
27
Provide Python code that opens a file named names.txt, in which each line of the file contains a friends name in the form firstname lastname, and displays each of the names on the screen, one per screen line.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
28
Provide Python code that prompts the user for the file name of the form filename.txt of a text file, opens the file, and displays each of the names on the screen, one per screen line.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
29
Provide Python code that prompts the user for the file name of the form filename.txt of a text file, opens the file, and writes the contents of the file to a new file name filename-2.txt.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
30
Any of the sequence methods of Python may be applied to strings.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
31
Strings are immutable in Python, and therefore cannot be altered.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
32
Any string method involving the modification of a string returns a new string containing the modification, with the original string the method is called on unchanged.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
33
Collectively, the operations performed on strings is called _______________________.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
34
The ________ string method in Python can be used to determine if a string contains only letters, whereas the __________ method can be used determine is a string contains only digits.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
35
The ________ and __________ string methods in Python can be used to determine if a string contains only lowercase or uppercase characters, whereas the __________ and ___________ methods can be used to convert a string to all lowercase or all uppercase characters.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
36
Give Python code that displays the longer of two names, name1 and name2.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
37
Give Python code that display "Valid SSN" if the string assigned to variable SSN is a valid social security number, and displays "Invalid SSN" otherwise. A social security number is valid if it contains exactly nine digits, and no other characters.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
38
Give Python code that removes any occurrences of dashes from a given social security number that variable SSN is assigned to, and updates SSN to contain the dash-removed result.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
39
Give Python code that, for any state of the U.S. assigned to variable state_name, displays the name ensuring that the first letter is displayed capitalized.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
40
Give Python code that, for a first name and last name assigned to variable full_name with the first and last name separated by a single space (e.g., Alice Morgan), displays the first name only.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
41
Give Python code that for a first name and last name assigned to variable full_name with the first and last name separated by a single space (e.g., Alice Morgan), assigns the first name to variable first_name and the last name to variable last_name.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
42
An exception is an object that is raised when an unexpected situation has occurred.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
43
All raised exceptions should be able to be caught and handled to keep a program executing.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
44
Python contains a predefined set of exceptions called standard exceptions.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
45
Exceptions that are thrown by a given function must be caught by the function to gracefully handle errors.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
46
Exceptions must be caught and handled by the calling code of the function throwing the exceptions.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
47
If an exception is thrown all the back to the main module and not handled there, the program terminates.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
48
The section of code making a call to any function that raises an exception must be contained in a try suite (block).
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
49
An exception handler is the section of code that is executed when a particular exception type is caught.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
50
Which of the following is not a standard exception in Python?

A) ImportError
B) IndexError
C) AssignmentError
D) TypeError
E) ValueError
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
51
An exception is a value (object) that is __________ when an exceptional situation occurs.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
52
The predefined exceptions in Python are called the ____________ exceptions.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
53
The expression 'Hello' + 10 would raise a _________________ exception on Python.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
54
The expression int('10A') would raise a ________________ exception on Python.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
55
55. The section of code that is to be executed when a particular exception has been caught is called an _____________________________.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
56
Give section of code that prompts the user for an integer value, and displays the number entered if no exception is raised, otherwise catches the exception when a non-numeric character is entered, displaying the message "non-digit found in input."
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
57
Write a function called getNum(start, end) that is passed the starting and ending values of a range of integers, prompts the user to enter an integer in the specified range, and returns the integer entered. The function should throw a ValueError exception if the entered number is not within the required range.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
58
Write a function called getYearOfBirth that prompts the user to enter their year of birth as a four-digit number, and returns the year entered. The function should throw a ValueError exception if the entered number is not within the range 1900 to the present year. Write a program that call this function, and keeps calling it until a valid year has been entered.
Unlock Deck
Unlock for access to all 58 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 58 flashcards in this deck.