Loading topic
open(). The call does not return the file's contents. It returns a file object, which is a handle you use to read, write, and close the file. This chapter explains what that object is, which layers it is built from, and what state it carries. Know7 min readstr and bytes, and can use try/except. The with statement is covered in the next chapter. Here the examples use it without explanation, because it closes the file for you.5 min readstr and bytes, and can use try/except/finally. It also assumes you have seen open() return a file object. Here you will learn why a file must be closed, what with does to guarantee that, and whic5 min readread(), readline(), readlines(), and iteration. It also covers how to choose between them, and the errors people commonly hit.6 min readopen() returns a file object, what the mode letters mean, and why the with statement is the standard way to close files. It also assumes you can use try/except. Here you will learn how text gets from your program into a file, and what differs between "w", 6 min readopen() and with. It also assumes you know the difference between str and bytes. Here you will learn how Python turns text into bytes and back, and how it handles line endings.6 min readpathlib represents file locations, how to build and take apart paths, and how to ask the filesystem about them. It assumes you can use open() with a with block and understand str values and try/except. Python 3.9 or newer is assumed for with_stem() and `is_relativ6 min readPath methods that read or write a whole file in one call: read_text, write_text, read_bytes, and write_bytes. It assumes you can use open() with with, you know the difference between str and bytes, you can build a Path with the / operator, and you c6 min readopen() and the with statement, and that you know how try/except selects an exception by type. It covers which exceptions file operations raise, how to catch the ones you can handle, and how to avoid hiding the ones you can't.5 min readopen() and with, you know the difference between str and bytes, and you can use try/except. It covers opening files in binary mode, what changes compared with text mode, and how to process files of any size in fixed-size pieces.7 min readopen(), use the with statement, and set an explicit encoding. It also assumes you know the difference between str and bytes, and that you can write a try/except block. The goal is to move structured data between Python objects and text6 min readread(), write() and readline() all start from it. This chapter covers the two methods that report and change that position, tell() and seek(). It also covers the restrictions text mode puts on them, and the patterns they enable: reading the end o6 min readopen() and with, write text with an explicit encoding, build paths with pathlib, and catch exceptions with try/except. It builds on the earlier chapters about write modes and error handling.6 min read