0% found this document useful (0 votes)
32 views

Python Fundamentals: Files and Resource Management

This document discusses Python file handling and resource management. It covers opening files with the open() function, specifying modes for reading, writing and appending text or binary files. It emphasizes the need to close files after use and introduces the with statement as a way to ensure clean up. The document also discusses file-like objects and context managers.

Uploaded by

VFisa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Python Fundamentals: Files and Resource Management

This document discusses Python file handling and resource management. It covers opening files with the open() function, specifying modes for reading, writing and appending text or binary files. It emphasizes the need to close files after use and introduces the with statement as a way to ensure clean up. The document also discusses file-like objects and context managers.

Uploaded by

VFisa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Python Fundamentals

Files and Resource Management

Robert Smallshire! Austin Bingham!


@robsmallshire! @austin_bingham!
[email protected] [email protected]
Presenter
open()
open a file
open() open a file

file: path to file (required)


open() open a file

file: path to file (required)


mode: read/write/append, binary/text
open() open a file

file: path to file (required)


mode: read/write/append, binary/text
encoding: text encoding
Binary File
Access

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

You might also like