Python Fundamentals: Files and Resource Management
Python Fundamentals: Files and Resource Management
write()
read()
Text File Access
encode write()
universal newlines
decode read()
open() modes
write() returns the
number of
codepoints, not the
number of
characters.
Typical File Use
f = open()!
# work work work!
f.close()
Typical File Use
f = open()!
# work work work!
f.close()
s e ( ) i s r e q u i r e d
cl o
to actu a l l y w r i te
th e d a ta !
with-block
resource cleanup with context-managers
with-block
resource cleanup with context-managers
r e tu r n s a
open()
te x t- m a n a g e r !
c o n
Moment of Zen
Beautiful is better
than ugly
Sugary syntax!
faultlessness attained through!
TODO: Need another
syllable!!
sweet fidelity
Moment of Zen
Beautiful is better
than ugly
with EXPR as
VAR:!
Sugary syntax! BLOCK
faultlessness attained through!
TODO: Need another
syllable!!
sweet fidelity
Moment of Zen
mgr = (EXPR) !
exit = type(mgr).__exit__ # Not calling it yet !
Beautiful is better
value = type(mgr).__enter__(mgr) !
exc = True !
try: !
than ugly
try: !
VAR = value # Only if "as VAR" iswith
BLOCK !
presentEXPR
! as
except: ! VAR:!
Sugary syntax!case is handled hereBLOCK
# The exceptional !
exc = False !
faultlessness attained through!
if not exit(mgr, *sys.exc_info()): !
TODO: raise Need
! another
# The exception is swallowed if exit() returns true !
syllable!!
finally: !
# The normal and non-local-goto cases are handled here !
sweet
if exc: !
fidelity
exit(mgr, None, None, None) !
Binary files
Device independent bitmaps
file-like objects
loosely-define set of behaviors for things that act like files
If
it looks like
a file and
reads like a
file, then it
Files and resource management
Summary
Files are opened using the built-in open() function which accepts a file mode to
control read/write/append behavior and whether the file is to be treated as raw
binary or encoded text data!
For text data you should specify a text encoding!
Text files deal with string objects and perform universal newline translation and
string encoding!
Binary files deal with bytes objects with no newline translation or encoding!
When writing files, it's our responsibility to provide newline characters for line
breaks!
Files should always be closed after use!
Files provide various line-oriented methods for reading, and are also iterators
which yield line by line!
Files are context managers and the with-statement can be used with context
managers to ensure that clean up operations, such as closing files, are performed!
The notion of file-like-objects is loosely defined, but very useful in practice!
Exercise EAFP to make the most of them!
Context managers aren't restricted to file-like-objects. We can use tools in the
contextlib standard library module, such as the closing() wrapper to create our
own context managers
Files and resource management
Summary
help() can be used on instance objects, not just types!
Python supports bitwise operators &, | and left- and right-shifts
Files and resource management
Summary
help() can be used on instance objects, not just types!
Python supports bitwise operators &, | and left- and right-shifts