open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) Open file and return a stream. Raise IOError upon failure. file is either a text or byte string giving the name (and the path if the file isn't in the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped. (If a file descriptor is given, it is closed when the returned I/O object is closed, unless closefd is set to False.) mode is an optional string that specifies the mode in which the file is opened. It defaults to 'r' which means open for reading in text mode. Other common values are 'w' for writing (truncating the file if it already exists), 'x' for creating and writing to a new file, and 'a' for appending (which on some Unix systems, means that all writes append to the end of the file regardless of the current seek position). In text mode, if encoding is not specified the encoding used is platform dependent: locale.getpreferredencoding(False) is called to get the current locale encoding. (For reading and writing raw bytes use binary mode and leave encoding unspecified.) The available modes are: ========= =============================================================== Character Meaning --------- --------------------------------------------------------------- 'r' open for reading (default) 'w' open for writing, truncating the file first 'x' create a new file and open it for writing 'a' open for writing, appending to the end of the file if it exists 'b' binary mode 't' text mode (default) '+' open a disk file for updating (reading and writing) 'U' universal newline mode (deprecated) ========= ===============================================================
Searching through a files
use str.startwidth:
1 2 3 4 5
fhand = open('mbox-short.txt') count = 0 for line in fhand: if line.startswith('From:'): print(line)
use str.find:
1 2 3 4 5
fhand = open('mbox-short.txt') for line in fhand: line = line.rstrip() if line.find('@uct.ac.za') == -1: continue print(line)
Writing files
open with mode w, If the file already exists, opening it in write mode clears out the old data and starts fresh, so be careful! If the file doesn’t exist, a new one is created.
write method does’t add the newline automatically.
using close to make sure the last bit of data is phsically written to the disk.
Debugging
use repr to display string whitch contain whitespace/tabs/newlines.
If there are whitespace/tabs/newlines charactor in a string, it’s hard to read, because of it’s invisable. Use built-in function repr can help:
1 2
>>> print(repr(s)) '1 2\t 3\n 4'
Lists
List is a sequence
A list whitin another list is nested.
A list the contains no elements is clled an empty list, use [] to create one.
Lists are mutable
If you try to read or write an element that does not exist, you get an IndexError.
If an index has a negative value, it counts backward from the end of the list.
A for loop over an empty list never executes the body.
List operations
The + operator concatenates lists
The * operator repeats a list a given number of time
List slices
1 2 3 4 5
>>> t = ['a', 'b', 'c', 'd', 'e', 'f'] >>> t[1:3] >>> t[:4] >>> t[3:] >>> t[:] #return all
A slice operator on the left side of an assignment can update multiple elements: