Open function
open
Function
open
FunctionOpens a file.
There are 3 errors in the following code:
The return value from open
is a file handle, given out from the operating system to your Python application. You will want to return this file handle once you’re finished with the file, if only so that your application won’t reach the limit of the number of open file handles it can have at once.
Explicitly calling close
closes the file handle, but only if the read was successful. If there is any error just after f = open(...)
, f.close()
will not be called (depending on the Python interpreter, the file handle may still be returned, but that’s another story).
To ensure that the file gets closed whether an exception occurs or not, pack it into a with
statement:
The first argument of open
is the filename.
The second one (the mode) determines how the file gets opened.
If you want to read the file, pass in
r
If you want to read and write the file, pass in
r+
If you want to overwrite the file, pass in
w
If you want to append to the file, pass in
a
The mode can contain one further character; we can open the file in binary (you’ll get a string of bytes) or text mode (a string of characters).
If you open something in text mode (i.e. add a t
, or nothing apart from r/r+/w/a
), you must also know which encoding to use. For a computer, all files are just bytes, not characters.
How do you find out which encoding a file you’re reading was written in? Well, unfortunately, there is no foolproof way to detect the encoding - the same bytes can represent different, but equally valid characters in different encodings. Therefore, you must rely on metadata (for example, in HTTP headers) to know the encoding. Increasingly, formats just define the encoding to be UTF-8.
Example
Write a program that reads a file, determines whether it’s JPG (hint: These files start with the bytes FF D8
), and writes a text file that describe the input file:
Last updated