1
2
3
4
5
6
7
8
9
10
11
12
To practice on your own, or to check code you believe shouldn't have been scored as incorrect, go to Trinket.
Post a review on Amazon for the paperback or the Kindle edition.
Email me to give me a compliment, complaint, or correction. I'll respond.
0,1,2,3,4,5,6,7,8,9,10,11
0
0
0
0
Rewrite this line to read the file. (Don't use the shorthand that I told you about.) | with open("x.txt", "w") as f: | with open("x.txt", "r") as f: | ^ *with open\(•x\.txt•, •r•\) as f: *$ |
Rewrite this line to read, not write to the file. Do use the shorthand I told you about.) | with open("x.txt", "w") as f: | with open("x.txt") as f: | ^ *with open\(•x\.txt•\) as f: *$ |
Open the file "y.txt" to read. The file handle is file_to_read. Use the long form, not the shorthand. | with open("y.txt", "r") as file_to_read: | with `open`(`"`y.txt`"`, `"`r`"`) `as `file_to_read`: | |
Open the file "y.txt" to read. The file handle is f. Use the the shorthand. | with open("y.txt") as f: | with open("y.txt") as f: | |
Read the contents of the file whose handle is f into the variable contents. Start by indenting. | contents = f.read() | ^ * contents = f\.read\(\) *$ | |
Read the file. Make up the variable. Start by indenting. | with open("students.txt") as student_file: | info = student_file.read() | ^ * [a-zA-Z_][a-zA-Z0-9_]* = student_file\.read\(\) *$ |
Open a file for reading using the long form. Then read it. Make everything up. | with open("students.txt", "r") as f: student_list = f.read() |
^ *with open\(•.+\.txt•, •r•\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n [a-zA-Z_][a-zA-Z0-9_]* = \1\.read\(\) *$ | |
Open a file for reading using the shorthand form. Then read it. Make everything up. | with open("students.txt") as f: student_list = f.read() |
^ *with open\(•[a-zA-Z_][a-zA-Z0-9_]*\.txt•\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n [a-zA-Z_][a-zA-Z0-9_]* = \1\.read\(\) *$ | |
Read this file. Use shorthand to open it. Make up the variable. | with open("students.txt", "w") as student_file student_file.write("Smith", "Brown", "Alain") |
with open("students.txt") as student_file: student_info = student_file.read() |
^ *with open\(•students\.txt•\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n [a-zA-Z_][a-zA-Z0-9_]* = \1\.read\(\) *$ |
Write to a file, then read it. Use the shorthand form. Make everything up. | with open("x.txt", "w") as f: f.write("Hello, World!") with open("x.txt") as my_file: info_in_file = my_file.read() |
^ *with open\(•(.+)\.txt•, •w•\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n \2\.write\(•.+•\)\nwith open\(•\1\.txt•\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n [a-zA-Z_][a-zA-Z0-9_]* = \3\.read\(\) *$ | |
|
|||