1/37
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
text file
contains data that has been encoded as text
binary file
contains data that has not been converted to text
open()
opens a file and returns it as a file object
file.close()
closes the file, after which no more reads or writes to the file are allowed
file.read()
returns the file contents as a string
file.readlines()
returns a list of strings, where the first element is the contents of the first line, the second is the contents of the second line, and so on
file.readline()
returns a single line at a time
file.write()
writes a string argument to a file
file.seek(offset)
change file’s current position
file.write(‘text’)
to write a string to the file
mode
indicates how a file is opened
“r”
open the file for reading
“w”
open the file for writing
if the file doesn’t exist, then the file is created
contents of an existing file are overwritten
“a”
open the file for appending
if the file doesn’t exist, then the file is created
writes are added to the end of existing file contents
What does the “+” modifier mean for modes?
it allows you to perform both reading and writing on the same file stream
“r+”
open for reading and writing without truncating the file; the pointer is at the beginning
“w+”
truncates the file to zero length or creates a new one for reading and writing
“a+”
open for. reading and writing, appends at the end, creates the file if it doesn’t exist
CSV (comma-separated values) file
a text file that contains data separated by a specific character
csv module
can be used to help read and write files in the csv format
output buffer
output to a file is buffered by the interpreter before being written to the computer’s hard disk, so there may be a delay between a call of write() and writing that data to the disk
file.flush()
manually forces the program to clear its internal memory buffer and write all pending data down to the operating system’s file buffer
exception
error that occurs while a program’s running, causing the program to abruptly halt
traceback
a lengthy error message that is shown in the sample run and gives information regarding the line number(s) that caused the exception
exception handling
process of responding to unexpected or unwanted events and errors during execution
exception handling format
try:
except:
else:
finally:
try:
wraps code that might cause an exception
except:
handles the specific exception raised
else:
executes if no exception occurs
finally:
executes regardless of exceptions; often used for cleanup
EOFError
input() hits an end-of-file condition (EOF) without reading any input
KeyError
a dictionary key is not found in the set of keys
ZeroDivisionError
divide by zero error
ValueError
invalid value (e.g. input mismatch)
IndexError
index is out of bounds
custom exception type
must define a new class that inherits from the built-in Exception class
custom exception type syntax
class MyCustomError(Exception):
"""Exception raised for specific application errors."""
pass