Python Standard Library Fredrik Lundh Download PDF
Python Standard Library Fredrik Lundh Download PDF
com
https://ebookultra.com/download/python-standard-
library-fredrik-lundh/
https://ebookultra.com/download/the-python-standard-library-by-
example-1st-edition-hellmann/
ebookultra.com
https://ebookultra.com/download/the-c-standard-library-2nd-edition-
nicolai-m-josuttis/
ebookultra.com
https://ebookultra.com/download/the-standard-ml-basis-library-1st-
edition-emden-r-gansner/
ebookultra.com
https://ebookultra.com/download/core-jstl-mastering-the-jsp-standard-
tag-library-1st-edition-geary/
ebookultra.com
One Discipline Four Ways Fredrik Barth
https://ebookultra.com/download/one-discipline-four-ways-fredrik-
barth/
ebookultra.com
https://ebookultra.com/download/we-are-all-leaders-1st-edition-
fredrik-arnander/
ebookultra.com
https://ebookultra.com/download/us-against-you-beartown-2-1st-edition-
fredrik-backman/
ebookultra.com
https://ebookultra.com/download/bioreactors-design-operation-and-
novel-applications-1st-edition-carl-fredrik-mandenius/
ebookultra.com
Python Standard Library Fredrik Lundh Digital Instant
Download
Author(s): Fredrik Lundh
ISBN(s): 9780596000967, 0596000960
Edition: Kindle
File Details: PDF, 1.81 MB
Year: 2001
Language: english
Preface
"We'd like to pretend that "Fredrik" is a role, but even hundreds of
volunteers couldn't possibly keep up. No, "Fredrik" is the result of
crossing an http server with a spam filter with an emacs whatsit and
some other stuff besides"
The Python 2.0 distribution comes with an extensive standard library, comprising over 200 modules.
This book provides a brief description of each module, plus one or more sample scripts showing how to
use it. All in all, this book contains 360 sample scripts.
Since I first stumbled upon Python some five years ago, I've spent hundreds of hours answering
questions on the comp.lang.python newsgroup. Maybe someone found a module that might be
exactly what he wanted, but he couldn't really figure out how to use it. Maybe someone had picked the
wrong module for the task. Or maybe someone tried to reinvent the wheel. Often, a short sample script
could be much more helpful than a pointer to the reference documentation.
After posting a couple of scripts each week, for a number of years, you end up with a rather large
collection of potentially useful scripts. What you'll find in this book are the best parts from over 3,000
newsgroup messages. You'll also find hundreds of new scripts added to make sure every little nook and
cranny of the standard library has been fully covered.
I've worked hard to make the scripts easy to understand and adaptable. I've intentionally kept the
annotations as short as possible. If you want more background, there's plenty of reference material
shipped with most Python distributions. In this book, the emphasis is on the code.
Comments, suggestions, and bug reports are welcome. Send them to [email protected]. I read
all mail as soon as it arrives, but it might take a while until I get around to answer.
For updates, addenda, and other information related to this book, point your favorite web browser to
http://www.pythonware.com/people/fredrik/librarybook.htm
This book covers the entire standard library, except the (optional) Tkinter user interface library. There
are several reasons for this, mostly related to time, space, and the fact that I'm working on several
other Tkinter documentation projects.
For current status on these projects, see http://www.pythonware.com/people/fredrik/tkinterbook.htm
Production details
This book was written in DocBook SGML. I used a variety of tools, including Secret Labs'
PythonWorks, and Excosoft Documentor, James Clark's Jade DSSSL processor, and Norm Walsh's
DocBook stylesheets. And a bunch of Python scripts, of course.
Thanks to my referees: Tim Peters, Guido van Rossum, David Ascher, Mark Lutz, and Rael Dornfest,
and the PythonWare crew: Matthew Ellis, Håkan Karlsson, and Rune Uhlin.
Thanks to Lenny Muellner, who turned my SGML files into the book you see before you, and to
Christien Shanreaw, who pulled all the different text and code files together for the book and the CD-
ROM.
Core Modules
"Since the functions in the C runtime library are not part of the Win32
API, we believe the number of applications that will be affected by this
bug to be very limited"
Overview
Python's standard library covers a wide range of modules. Everything from modules that are as much a
part of the Python language as the types and statements defined by the language specification, to
obscure modules that are probably useful only to a small number of programs.
This section describes a number of fundamental standard library modules. Any larger Python program
is likely to use most of these modules, either directly or indirectly.
Two modules are even more basic than all other modules combined: the __builtin__ module defines
built-in functions (like len, int, and range), and the exceptions module defines all built-in
exceptions.
Python imports both modules when it starts up, and makes their content available for all programs.
Several built-in types have support modules in the standard library. The string module implements
commonly used string operations, the math module provides math operations and constants, and the
cmath module does the same for complex numbers.
Regular Expressions
The re module provides regular expressions support for Python. Regular expressions are string
patterns written in a special syntax, which can be used to match strings, and extract substrings.
sys gives you access to various interpreter variables, such as the module search path, and the
interpreter version. operator provides functional equivalents to many built-in operators. copy allows
you to copy objects. And finally, gc gives you more control over the garbage collector facilities in
Python 2.0.
Python Standard Library: Core Modules 1-3
whither canada?
15
To pass keyword arguments to a function, you can use a dictionary as the third argument to apply:
crunchy frog
crunchy frog
crunchy frog
Python Standard Library: Core Modules 1-4
One common use for apply is to pass constructor arguments from a subclass on to the base class,
especially if the constructor takes a lot of arguments.
class Rectangle:
def __init__(self, color="white", width=10, height=10):
print "create a", color, self, "sized", width, "x", height
class RoundedRectangle(Rectangle):
def __init__(self, **kw):
apply(Rectangle.__init__, (self,), kw)
Python 2.0 provides an alternate syntax. Instead of apply, you can use an ordinary function call, and
use * to mark the tuple, and ** to mark the dictionary.
The following two statements are equivalent:
result = function(*args, **kwargs)
result = apply(function, args, kwargs)
The trick is that you can actually call this function directly. This can be handy if you have the module
name in a string variable, like in the following example, which imports all modules whose names end
with "-plugin":
import glob, os
modules = []
Note that the plugin modules have hyphens in the name. This means that you cannot import such a
module using the ordinary import command, since you cannot have hyphens in Python identifiers.
Here's the plugin used in this example:
def hello():
print "example-plugin says hello"
The following example shows how to get a function object, given that you have the module and
function name as strings:
You can also use this function to implement lazy loading of modules. In the following example, the
string module is imported when it is first used:
class LazyImport:
def __init__(self, module_name):
self.module_name = module_name
self.module = None
def __getattr__(self, name):
if self.module is None:
self.module = __import__(self.module_name)
return getattr(self.module, name)
string = LazyImport("string")
print string.lowercase
abcdefghijklmnopqrstuvwxyz
Python provides some basic support for reloading modules that you've already imported. The following
example loads the hello.py file three times:
import hello
reload(hello)
reload(hello)
reload uses the module name associated with the module object, not the variable name. This means
that even if you've renamed the module, reload will still be able to find the original module.
Note that when you reload a module, it is recompiled, and the new module replaces the old one in the
module dictionary. However, if you have created instances of classes defined in that module, those
instances will still use the old implementation.
Likewise, if you've used from-import to create references to module members in other modules,
those references will not be updated.
Python Standard Library: Core Modules 1-7
Looking in namespaces
The dir function returns a list of all members of a given module, class, instance, or other type. It's
probably most useful when you're working with an interactive Python interpreter, but can also come in
handy in other situations.
def dump(value):
print value, "=>", dir(value)
import sys
dump(0)
dump(1.0)
dump(0.0j) # complex number
dump([]) # list
dump({}) # dictionary
dump("string")
dump(len) # function
dump(sys) # module
0 => []
1.0 => []
0j => ['conjugate', 'imag', 'real']
[] => ['append', 'count', 'extend', 'index', 'insert',
'pop', 'remove', 'reverse', 'sort']
{} => ['clear', 'copy', 'get', 'has_key', 'items',
'keys', 'update', 'values']
string => []
<built-in function len> => ['__doc__', '__name__', '__self__']
<module 'sys' (built-in)> => ['__doc__', '__name__',
'__stderr__', '__stdin__', '__stdout__', 'argv',
'builtin_module_names', 'copyright', 'dllhandle',
'exc_info', 'exc_type', 'exec_prefix', 'executable',
...
In the following example, the getmember function returns all class-level attributes and methods
defined by a given class:
class A:
def a(self):
pass
def b(self):
pass
Python Standard Library: Core Modules 1-8
class B(A):
def c(self):
pass
def d(self):
pass
print getmembers(A)
print getmembers(B)
print getmembers(IOError)
Note that the getmembers function returns an ordered list. The earlier a name appears in the list, the
higher up in the class hierarchy it's defined. If order doesn't matter, you can use a dictionary to collect
the names instead of a list.
The vars function is similar, but it returns a dictionary containing the current value for each member.
If you use it without an argument, it returns a dictionary containing what's visible in the current local
namespace:
book = "library2"
pages = 250
scripts = 350
print "the %(book)s book contains more than %(scripts)s scripts" % vars()
Python is a dynamically typed language, which means that a given variable can be bound to values of
different types at different occasions. In the following example, the same function is called with an
integer, a floating point value, and a string:
def function(value):
print value
function(1)
function(1.0)
function("one")
The type function allows you to check what type a variable has. This function returns a type
descriptor, which is a unique object for each type provided by the Python interpreter.
def dump(value):
print type(value), value
dump(1)
dump(1.0)
dump("one")
<type 'int'> 1
<type 'float'> 1.0
<type 'string'> one
Each type has a single corresponding type object, which means that you can use the is operator (object
identity) to do type testing:
Example: Using the type function to distinguish between file names and file objects
# File:builtin-type-example-2.py
def load(file):
if isinstance(file, type("")):
file = open(file, "rb")
return file.read()
4672 bytes
4672 bytes
Python Standard Library: Core Modules 1-10
The callable function checks if an object can be called (either directly or via apply). It returns true for
functions, methods, lambda expressions, classes, and class instances which define the __call__
method.
def dump(function):
if callable(function):
print function, "is callable"
else:
print function, "is *not* callable"
class A:
def method(self, value):
return value
class B(A):
def __call__(self, value):
return value
a = A()
b = B()
dump(A) # classes
dump(B)
dump(B.method)
dump(a) # instances
dump(b)
dump(b.method)
0 is *not* callable
string is *not* callable
<built-in function callable> is callable
<function dump at 8ca320> is callable
A is callable
B is callable
<unbound method A.method> is callable
<A instance at 8caa10> is *not* callable
<B instance at 8cab00> is callable
<method A.method of B instance at 8cab00> is callable
Note that the class objects (A and B) are both callable; if you call them, they create new objects.
However, instances of class A are not callable, since that class doesn't have a __call__ method.
You'll find functions to check if an object is of any of the built-in number, sequence, or dictionary types
in the operator module. However, since it's easy to create a class that implements e.g. the basic
sequence methods, it's usually a bad idea to use explicit type testing on such objects.
Python Standard Library: Core Modules 1-11
Things get even more complicated when it comes to classes and instances. Python doesn't treat classes
as types per se. Instead, all classes belong to a special class type, and all class instances belong to a
special instance type.
This means that you cannot use type to test if an instance belongs to a given class; all instances have
the same type! To solve this, you can use the isinstance function, which checks if an object is an
instance of a given class (or of a subclass to it).
class A:
pass
class B:
pass
class C(A):
pass
def dump(object):
print object, "=>",
if isinstance(object, A):
print "A",
if isinstance(object, B):
print "B",
if isinstance(object, C):
print "C",
if isinstance(object, D):
print "D",
print
a = A()
b = B()
c = C()
d = D()
dump(a)
dump(b)
dump(c)
dump(d)
dump(0)
dump("string")
The issubclass function is similar, but checks whether a class object is the same as a given class, or is
a subclass of it.
Note that while isinstance accepts any kind of object, the issubclass function raises a TypeError
exception if you use it on something that is not a class object.
class A:
pass
class B:
pass
class C(A):
pass
def dump(object):
print object, "=>",
if issubclass(object, A):
print "A",
if issubclass(object, B):
print "B",
if issubclass(object, C):
print "C",
if issubclass(object, D):
print "D",
print
dump(A)
dump(B)
dump(C)
dump(D)
dump(0)
dump("string")
A => A
B => B
C => A C
D => A B D
0 =>
Traceback (innermost last):
File "builtin-issubclass-example-1.py", line 29, in ?
File "builtin-issubclass-example-1.py", line 15, in dump
TypeError: arguments must be classes
Python Standard Library: Core Modules 1-13
Python provides several ways to interact with the interpreter from within a program. For example, the
eval function evaluates a string as if it were a Python expression. You can pass it a literal, simple
expressions, or even use built-in functions:
def dump(expression):
result = eval(expression)
print expression, "=>", result, type(result)
dump("1")
dump("1.0")
dump("'string'")
dump("1.0 + 2.0")
dump("'*' * 10")
dump("len('world')")
A problem with eval is that if you cannot trust the source from which you got the string, you may get
into trouble. For example, someone might use the built-in __import__ function to load the os
module, and then remove files on your disk:
print eval("__import__('os').getcwd()")
print eval("__import__('os').remove('file')")
/home/fredrik/librarybook
Traceback (innermost last):
File "builtin-eval-example-2", line 2, in ?
File "<string>", line 0, in ?
os.error: (2, 'No such file or directory')
Note that you get an os.error exception, which means that Python actually tried to remove the file!
Python Standard Library: Core Modules 1-14
Luckily, there's a way around this problem. You can pass a second argument to eval, which should
contain a dictionary defining the namespace in which the expression is evaluated. Let's pass in an
empty namespace:
If you print the contents of the namespace variable, you'll find that it contains the full set of built-in
functions.
The solution to this little dilemma isn't far away: since Python doesn't add this item if it is already
there, you just have to add a dummy item called __builtins__ to the namespace before calling eval:
/home/fredrik/librarybook
Traceback (innermost last):
File "builtin-eval-example-3.py", line 2, in ?
File "<string>", line 0, in ?
NameError: __import__
Note that this doesn't product you from CPU or memory resource attacks (for example, something like
eval("'*'*1000000*2*2*2*2*2*2*2*2*2") will most likely cause your program to run out of
memory after a while)
Python Standard Library: Core Modules 1-15
The eval function only works for simple expressions. To handle larger blocks of code, use the compile
and exec functions:
NAME = "script.py"
BODY = """
prnt 'owl-stretching time'
"""
try:
compile(BODY, NAME, "exec")
except SyntaxError, v:
print "syntax error:", v, "in", NAME
When successful, the compile function returns a code object, which you can execute with the exec
statement:
BODY = """
print 'the ant, an introduction'
"""
print code
exec code
To generate code on the fly, you can use the class shown in the following example. Use the write
method to add statements, and indent and dedent to add structure, and this class takes care of the
rest.
class CodeGeneratorBackend:
"Simple code generator for Python"
def end(self):
self.code.append("") # make sure there's a newline at the end
return compile(string.join(self.code, "\n"), "<code>", "exec")
def indent(self):
self.level += 1
# in Python 1.5.2 and earlier, use this instead:
# self.level = self.level + 1
def dedent(self):
if self.level == 0:
raise SyntaxError, "internal error in code generator"
self.level -= 1
# in Python 1.5.2 and earlier, use this instead:
# self.level = self.level - 1
#
# try it out!
c = CodeGeneratorBackend()
c.begin()
c.write("for i in range(5):")
c.indent()
c.write("print 'code generation made easy!'")
c.dedent()
exec c.end()
Python also provides a function called execfile. It's simply a shortcut for loading code from a file,
compiling it, and executing it. The following example shows how to use and emulate this function.
execfile("hello.py")
EXECFILE("hello.py")
The hello.py file used in this example has the following contents:
Since Python looks among the built-in functions after it has checked the local and module namespace,
there may be situations when you need to explicitly refer to the __builtin__ module. For example,
the following script overloads the open function with a version that opens an ordinary file and checks
that it starts with a "magic" string. To be able to use the original open function, it explicitly refers to it
using the module name.
Python Standard Library: Core Modules 1-18
fp = open("samples/sample.gif")
print len(fp.read()), "bytes"
fp = open("samples/sample.jpg")
print len(fp.read()), "bytes"
3565 bytes
Traceback (innermost last):
File "builtin-open-example-1.py", line 12, in ?
File "builtin-open-example-1.py", line 5, in open
IOError: not a GIF file
Python Standard Library: Core Modules 1-19
You can create your own exception classes. Just inherit from the built-in Exception class (or a proper
standard exception), and override the constructor and/or __str__ method as necessary.
Python Standard Library: Core Modules 1-21
class HTTPError(Exception):
# indicates an HTTP protocol error
def __init__(self, url, errcode, errmsg):
self.url = url
self.errcode = errcode
self.errmsg = errmsg
def __str__(self):
return (
"<HTTPError for %s: %s %s>" %
(self.url, self.errcode, self.errmsg)
)
try:
raise HTTPError("http://www.python.org/foo", 200, "Not Found")
except HTTPError, error:
print "url", "=>", error.url
print "errcode", "=>", error.errcode
print "errmsg", "=>", error.errmsg
raise # reraise exception
The os module
This module provides a unified interface to a number of operating system functions.
Most of the functions in this module are implemented by platform specific modules, such as posix and
nt. The os module automatically loads the right implementation module when it is first imported.
import os
import string
try:
# remove old temp file, if any
os.remove(temp)
except os.error:
pass
fi = open(file)
fo = open(temp, "w")
for s in fi.readlines():
fo.write(string.replace(s, search_for, replace_with))
fi.close()
fo.close()
try:
# remove old backup file, if any
os.remove(back)
except os.error:
pass
#
# try it out!
file = "samples/sample.txt"
import os
sample.au
sample.jpg
sample.wav
...
The getcwd and chdir functions are used to get and set the current directory:
import os
# go down
os.chdir("samples")
print "2", os.getcwd()
# go back up
os.chdir(os.pardir)
print "3", os.getcwd()
1 /ematter/librarybook
2 /ematter/librarybook/samples
3 /ematter/librarybook
Python Standard Library: Core Modules 1-24
The makedirs and removedirs functions are used to create and remove directory hierarchies.
Example: Using the os module to create and remove multiple directory levels
# File:os-example-6.py
import os
os.makedirs("test/multiple/levels")
fp = open("test/multiple/levels/file", "w")
fp.write("inspector praline")
fp.close()
Note that removedirs removes all empty directories along the given path, starting with the last
directory in the given path name. In contrast, the mkdir and rmdir functions can only handle a single
directory level.
import os
os.mkdir("test")
os.rmdir("test")
To remove non-empty directories, you can use the rmtree function in the shutil module.
The stat function fetches information about an existing file. It returns a 9-tuple which contains the
size, inode change timestamp, modification timestamp, and access privileges.
Python Standard Library: Core Modules 1-25
import os
import time
file = "samples/sample.jpg"
def dump(st):
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st
print "- size:", size, "bytes"
print "- owner:", uid, gid
print "- created:", time.ctime(ctime)
print "- last accessed:", time.ctime(atime)
print "- last modified:", time.ctime(mtime)
print "- mode:", oct(mode)
print "- inode/dev:", ino, dev
#
# get stats for a filename
st = os.stat(file)
#
# get stats for an open file
fp = open(file)
st = os.fstat(fp.fileno())
stat samples/sample.jpg
- size: 4762 bytes
- owner: 0 0
- created: Tue Sep 07 22:45:58 1999
- last accessed: Sun Sep 19 00:00:00 1999
- last modified: Sun May 19 01:42:16 1996
- mode: 0100666
- inode/dev: 0 2
fstat samples/sample.jpg
- size: 4762 bytes
- owner: 0 0
- created: Tue Sep 07 22:45:58 1999
- last accessed: Sun Sep 19 00:00:00 1999
- last modified: Sun May 19 01:42:16 1996
- mode: 0100666
- inode/dev: 0 0
Python Standard Library: Core Modules 1-26
Some fields don't make sense on non-Unix platforms; for example, the (inode, dev) tuple provides a
unique identity for each file on Unix, but can contain arbitrary data on other platforms.
The stat module contains a number of useful constants and helper functions for dealing with the
members of the stat tuple. Some of these are shown in the examples below.
You can modify the mode and time fields using the chmod and utime functions:
import os
import stat, time
infile = "samples/sample.jpg"
outfile = "out.jpg"
# copy contents
fi = open(infile, "rb")
fo = open(outfile, "wb")
while 1:
s = fi.read(10000)
if not s:
break
fo.write(s)
fi.close()
fo.close()
original =>
mode 0666
atime Thu Oct 14 15:15:50 1999
mtime Mon Nov 13 15:42:36 1995
copy =>
mode 0666
atime Thu Oct 14 15:15:50 1999
mtime Mon Nov 13 15:42:36 1995
Python Standard Library: Core Modules 1-27
The system function runs a new command under the current process, and waits for it to finish.
import os
if os.name == "nt":
command = "dir"
else:
command = "ls -l"
os.system(command)
The command is run via the operating system's standard shell, and returns the shell's exit status.
Under Windows 95/98, the shell is usually command.com whose exit status is always 0.
Warning:
The exec function starts a new process, replacing the current one ("go to process", in other words). In
the following example, note that the "goodbye" message is never printed:
import os
import sys
program = "python"
arguments = ["hello.py"]
Python provides a whole bunch of exec functions, with slightly varying behavior. The above example
uses execvp, which searches for the program along the standard path, passes the contents of the
Python Standard Library: Core Modules 1-28
second argument tuple as individual arguments to that program, and runs it with the current set of
environment variables. See the Python Library Reference for more information on the other seven
ways to call this function.
Under Unix, you can call other programs from the current one by combining exec with two other
functions, fork and wait. The former makes a copy of the current process, the latter waits for a child
process to finish.
import os
import sys
run("python", "hello.py")
print "goodbye"
The fork returns zero in the new process (the return from fork is the first thing that happens in that
process!), and a non-zero process identifier in the original process. Or in other words, "not pid" is true
only if we're in the new process.
fork and wait are not available on Windows, but you can use the spawn function instead.
Unfortunately, there's no standard version of spawn that searches for an executable along the path, so
you have to do that yourself:
import os
import string
run("python", "hello.py")
Python Standard Library: Core Modules 1-29
print "goodbye"
You can also use spawn to run other programs in the background. The following example adds an
optional mode argument to the run function; when set to os.P_NOWAIT, the script doesn't wait for
the other program to finish.
The default flag value os.P_WAIT tells spawn to wait until the new process is finished. Other flags
include os.P_OVERLAY which makes spawn behave like exec, and os.P_DETACH which runs
the new process in the background, detached from both console and keyboard.
Example: Using the os module to run another program in the background (Windows)
# File:os-spawn-example-2.py
import os
import string
goodbye
hello again, and welcome to the show
Python Standard Library: Core Modules 1-30
The following example provides a spawn method that works on either platform:
import os
import string
#
# try it out!
spawn("python", "hello.py")
print "goodbye"
The above example first attempts to call a function named spawnvp. If that doesn't exist (it doesn't, in
2.0 and earlier), the function looks for a function named spawnv and searches the path all by itself. As
a last resort, it falls back on exec and fork.
Python Standard Library: Core Modules 1-31
On Unix, fork can also be used to turn the current process into a background process (a "daemon").
Basically, all you need to do is to fork off a copy of the current process, and terminate the original
process:
import os
import time
pid = os.fork()
if pid:
os._exit(0) # kill original
However, it takes a bit more work to create a real daemon. First, call setpgrp to make the new process
a "process group leader". Otherwise, signals sent to a (by that time) unrelated process group might
cause problems in your daemon:
os.setpgrp()
It's also a good idea to remove the user mode mask, to make sure files created by the daemon actually
gets the mode flags specified by the program:
os.umask(0)
Then, you should redirect the stdout/stderr files, instead of just closing them. If you don't do this, you
may get unexpected exceptions the day some of your code tries to write something to the console via
stdout or stderr.
class NullDevice:
def write(self, s):
pass
sys.stdin.close()
sys.stdout = NullDevice()
sys.stderr = NullDevice()
In other words, while Python's print and C's printf/fprintf won't crash your program if the devices
have been disconnected, sys.stdout.write() happily throws an IOError exception when the
application runs as a daemon. But your program works just fine when running in the foreground...
Python Standard Library: Core Modules 1-32
By the way, the _exit function used in the examples above terminates the current process. In contrast
to sys.exit, this works also if the caller happens to catch the SystemExit exception:
import os
import sys
try:
sys.exit(1)
except SystemExit, value:
print "caught exit(%s)" % value
try:
os._exit(2)
except SystemExit, value:
print "caught exit(%s)" % value
print "bye!"
caught exit(1)
Python Standard Library: Core Modules 1-33
import os
filename = "my/little/pony"
using nt ...
split => ('my/little', 'pony')
splitext => ('my/little/pony', '')
dirname => my/little
basename => pony
join => my/little\pony
This module also contains a number of functions that allow you to quickly figure out what a filename
represents:
import os
FILES = (
os.curdir,
"/",
"file",
"/file",
"samples",
"samples/sample.jpg",
"directory/file",
"../directory/file",
"/directory/file"
)
The expanduser function treats a user name shortcut in the same way as most modern Unix shells (it
doesn't work well on Windows).
Example: Using the os.path module to insert the user name into a filename
# File:os-path-expanduser-example-1.py
import os
print os.path.expanduser("~/.pythonrc")
/home/effbot/.pythonrc
import os
os.environ["USER"] = "user"
print os.path.expandvars("/home/$USER/config")
print os.path.expandvars("$USER/folders")
/home/user/config
user/folders
The walk function helps you find all files in a directory tree. It takes a directory name, a callback
function, and a data object that is passed on to the callback.
import os
The walk function has a somewhat obscure user interface (maybe it's just me, but I can never
remember the order of the arguments). The index function in the next example returns a list of
filenames instead, which lets you use a straightforward for-in loop to process the files:
import os
def index(directory):
# like os.listdir, but traverses directory trees
stack = [directory]
files = []
while stack:
directory = stack.pop()
for file in os.listdir(directory):
fullname = os.path.join(directory, file)
files.append(fullname)
if os.path.isdir(fullname) and not os.path.islink(fullname):
stack.append(fullname)
return files
.\aifc-example-1.py
.\anydbm-example-1.py
.\array-example-1.py
...
Python Standard Library: Core Modules 1-37
If you don't want to list all files (for performance or memory reasons), the following example uses a
different approach. Here, the DirectoryWalker class behaves like a sequence object, returning one
file at a time:
import os
class DirectoryWalker:
# a forward iterator that traverses a directory tree
.\aifc-example-1.py
.\anydbm-example-1.py
.\array-example-1.py
...
Note that this class doesn't check the index passed to the __getitem__ method. This means that it
won't do the right thing if you access the sequence members out of order.
Python Standard Library: Core Modules 1-38
Finally, if you're interested in the file sizes or timestamps, here's a version of the class that returns both
the filename and the tuple returned from os.stat. This version saves one or two stat calls for each file
(both os.path.isdir and os.path.islink uses stat), and runs quite a bit faster on some platforms.
Example: Using a directory walker to traverse a file system, returning both the filename
and additional file information
# File:os-path-walk-example-4.py
class DirectoryStatWalker:
# a forward iterator that traverses a directory tree, and
# returns the filename and additional file information
.\aifc-example-1.py 336
.\anydbm-example-1.py 244
.\array-example-1.py 526
Python Standard Library: Core Modules 1-39
import stat
import os, time
st = os.stat("samples/sample.txt")
import string
In Python 1.5.2 and earlier, this module uses functions from the strop implementation module where
possible.
In Python 1.6 and later, most string operations are made available as string methods as well, and many
functions in the string module are simply wrapper functions that call the corresponding string
method.
Example: Using string methods instead of string module functions (Python 1.6 and later)
# File:string-example-2.py
In addition to the string manipulation stuff, the string module also contains a number of functions
which convert strings to other types:
import string
print int("4711"),
print string.atoi("4711"),
print string.atoi("11147", 8), # octal
print string.atoi("1267", 16), # hexadecimal
print string.atoi("3mv", 36) # whatever...
print float("4711"),
print string.atof("1"),
print string.atof("1.23e5")
In most cases (especially if you're using 1.6 or later), you can use the int and float functions instead of
their string module counterparts.
The atoi function takes an optional second argument, which specifices the number base. If the base is
zero, the function looks at the first few characters before attempting to interpret the value: if "0x", the
base is set to 16 (hexadecimal), and if "0", the base is set to 8 (octal). The default is base 10 (decimal),
just as if you hadn't provided an extra argument.
In 1.6 and later, the int also accepts a second argument, just like atoi. But unlike the string versions,
int and float also accepts Unicode strings.
Python Standard Library: Core Modules 1-42
The re module
"Some people, when confronted with a problem, think "I know, I'll use
regular expressions." Now they have two problems"
This module provides a set of powerful regular expression facilities. A regular expression is a string
pattern written in a compact (and quite cryptic) syntax, and this module allows you to quickly check
whether a given string matches a given pattern (using the match function), or contains such a pattern
(using the search function).
The match function attempts to match a pattern against the beginning of the given string. If the
pattern matches anything at all (including an empty string, if the pattern allows that!), match returns
a match object. The group method can be used to find out what matched.
import re
# a single character
m = re.match(".", text)
if m: print repr("."), "=>", repr(m.group(0))
# a string of digits
m = re.match("\d+", text)
if m: print repr("\d+"), "=>", repr(m.group(0))
Edith Neil.
THE MAN ON A BICYCLE.
The man on a bicycle came panting up a hill at the beginning of a
large town.
“Hello! Knickerbockers,” cried the man on foot; “do you call that
the gait for a scorcher? Why, it aint more’n a pair uv bars.”
“That’s all right, little boy,” returned he of the wheel.
“Little boy, yourself! Didn’t you know it was five dollars fine for
ridin’ on the sidewalk?”
“Is it? All right? I’ll pay when I come back;” and the man on a
bicycle, encountering a level piece of road, put an end to further
conversation by a sudden spurt.
But the cyclometer was not to make a steady advance that
evening. A surface crossing lay ahead, blocked by a belated freight
train. The engineer knew his business and meddled continually with
the throttle. After going about two car-lengths in one direction, the
train would stop, remember something left behind, and back up. That
is the way to keep a crowd pacific. Give them plenty to hope for and
they forget to fight.
The wheelman rode in slow circles for a while, but finding the
slush and snow too deep for this exercise, was forced to a
humiliating dismount.
“Misder!” shouted a dirty urchin with a cold, “did yer know yer
’adn’d god no lighd? Fibe dollars fine an’ the cop’s in the deepo.”
“O, break away, break away!” snarled the wheelman.
“Young man,” lisped a willy boy, “I thought those things weah
called in, you know.”
“I wish some one would call that thing in.”
This retort was pointed at the willy boy, and raised a laugh.
“How long are they going to keep us waiting here in the cold?”
muttered a querulous old gentleman. “It’s against the law, and the
company ought to be prosecuted.”
“The present company?” ventured a bashful young man, who was
dressed as if going somewhere.
“No, the present company is always excepted,” came from
obscurity.
The man with a bicycle snapped his bell uneasily. He was in a
hurry, of course; if you live much on a wheel, hurry becomes chronic,
engendered perhaps by the accustomed sense of rapid motion; but,
like many of his class, he had that fellow feeling for petty law
breakers, which comes by taking chances against city ordinances.
The fellow at the valve was taking his chances too, with excellent
success. The patience of an American crowd approaches the
miraculous. Fifty engagements were being broken and ten times as
many toes were freezing, all because one railroader was too lazy to
draw a coupling-pin. Yet so long as the cars continued to move, no
one felt called upon to interfere.
“How many minutes may a crossing legally be blocked?”
demanded the querulous old gentleman, pulling out his watch.
“Ten, I believe,” answered the flagman, soothingly.
“Ten? Why, we’ve been here most fifteen now!”
“S’posin’ that train on the down track ud move up just as this un
was movin’ away, which ud you have ’rested then?”
The querulous old gentleman looked at the newsboy reprovingly,
but said nothing.
“Might try and have the president pulled,” suggested some one.
“What of? The Road? Wopey dick! He’s got a pull himself.”
The newsboy smiled approvingly upon his mot, during a silence
that might be felt. It was a relief when the wind picked up the tones
of a brass band, playing in front of the theatre, and wafted them in
that direction.
“Sub ud oughd to pud runners on thad bike, see?” volunteered the
dirty urchin with a cold.
This aroused the newsboy to a stroke of business.
“New Yawk Evening Sun or Worl! One cent! Sunorworl?”
Here the bashful young man who was dressed as if going
somewhere, separated himself, and cried:
“Conductor, cut this train in two, or I will have you arrested.”
“There, you’re done!”
“Cut it short!”
“Go, take a walk!”
were expressions which greeted this sally.
The bashful young man took up the thread of his private life where
it had broken off, and wished he had separated himself further.
A touch on the sleeve aroused the man with a bicycle. There stood
the man on foot.
“Hello! Knickerbockers. Horse tied, eh? Thought I’d ketch up to
you. Where is your century run? Didn’t I say that there was no
scorchin’ gait?”
The man with a bicycle said something that commenced with
“damn,” and then, seeing a pale frightened-looking girl near by,
wished he hadn’t. In the forgetfulness of his remorse he smirched
the newsboy with his machine.
“You most certainly want to get done hittin’ me with that there last
year’s safety,” began the latter, speaking loud enough to be heard by
all—but his philippic was cut short by the arrival of train orders, and
the clearing of the road. The man with a bicycle did a handsome
pedal mount and spun skillfully through the surging mob, catching
cries of “See that burning safety!” “Gimmie a ride, boss?” and the
like, from those left behind. But the man on a wheel continued to
ride.
The man on foot continued to walk.
And the band played on.
Harvey Lewis Wickham.
THE STEWARD.
A BALLAD OF DEATH.
C. P. N.
LET THERE BE GALL ENOUGH IN
THY INK.—Twelfth Night.
’Tis an odd old world, this of ours that is round like an orange, and
slightly flattened at the poles. It drives not well, and it hath but
moderate fondness for gall. Why, then, seek to drive it? Why harass
it with the hurtful attrition of gall-dippen pens? For in truth, its small
love for gall is yet greater than its use therefor. It needs not that we
should make it smart. ’Tis smart enough, and clever enough, already,
towering the hearts of the angels, and affording a spectacle for the
little fishes. Application of gall will not help it. Rightly used, a little
may ease thy own jaundice, but in thy ink it erodes the pen that uses
it.
Are we vexed at the follies of the round old world? Is our taste
offended by the unripe things written and painted and sung by those
who are not perfect as we are? We ought to consider the words of
the gentle stoic: “For it is natural that these things should be done by
such persons. It is a matter of necessity, and if a man will not have it
so he will not allow the fig-tree to have juice.”
To what end do we Philistines write? Is it not because we are
seeking after the truth? Is it not for the expression of that which to us
seems beautiful and helpful and desirable to be in the world?
And for what, let us suppose, do the armies of the aliens put pen
to paper? Is it not for the same end? And if they see not purely, do
we then hope to clarify their vision with gall? Rather we ought not to
thwart them in the pursuit of that which seems to them excellent and
worthy of effort. It is unphilosophic, and in contravention to the
scientific spirit, to deny others the right which we claim for ourselves.
We ought rather to do the thing which to us seems lovely, and let
that protest, for us, against unloveliness, by its life and realty—the
only effectual protest this world has ever known. Already there is too
much of strife, too much of denial in the world. Nature argues the
questioning life in the affirmative, for well she knows, the ancient
wise one, that denial is deadly. “I believe” is the password into the
secret places of good.
The single vision, that sees truest, the simple heart, that loves it;
the direct thought that sends it forth to bless—the world needs these
more than that we should camp upon its trail with gall in our ink and
our pens tipped with bitterness.
Gall can never fill a vacuum. “If you don’t want a boy to do that,”
said a wise teacher (putting thumb to nose), “teach him something
prettier.”
This, then, must we do for this hulking schoolboy world of ours; the
half-grown, growing world, that knows enough to recognize that gall
is neither good nor beautiful, yet sees not that the thing it does is, as
well, unlovely.
Let us strive to look out on life with open vision and simple soul,
seeing that it is fair; that no plant cometh forth to face the winter’s
blight, but when the winter is over then green, tender things of
beauty push upwards to meet the sunshine—first the blade, then the
ear, and after that the full corn in the ear. Then cometh the insect,
with his drop of gall, and ugliness and excrescence follow.
That which gives heat and light and blessing; that which generates
force, and yields beauty in the burning wood that cheers our hearth,
is the warm brightness of the sun’s rays that, in the process of
liberating oxygen into the outer air, were stored up in the growing
tree. If these had not been actually caught and incorporated in the
wood by Nature’s subtle chemistry, do you think the fire would warm
us, sitting by it?
And that in our literature, and in our art, that shall make them real,
and able to offer coming ages anything in the warmth and light of
this, must be what we shall manage to incorporate with their growth
of the pure and wholesome, the life-making forces of our time.
Negation is not life. Disease is not power. Abnormality is not truth.
These are forces that make for death; and life, in its scientific
ultimate, is the sum-total of the forces that resist death. So, I say
again, that work of ours which is to carry life forward, which is to
warm and light the ages, must be what we can perpetuate of the life-
giving, growing elements of this one. Let there be gall enough in thy
ink. To what purpose? Gall is not a promoter of growth. It is a result
of hurt, and where it touches it leaves even a bad thing worse than it
found it.
Adeline Knapp.
THE WORSHIPPERS.
Now it came to pass in the still night watches, when my body was
asleep, that my soul dreamed a dream.
And in my dream I heard a voice say, “Unstop his ears that he may
hear.”
And I became aware of the presence of an Angel, and he touched
mine ears, saying, “When thou hearest a sound, a great sound, as of
many mighty waters rushing headlong, listen, and fear nothing.”
Then, verily, did burst on my hearing a mighty noise, a most
discordant frush, and I stretched out my hand to the Angel, who said,
“Fear not! Now tell me what thou hearest.”
After pondering a long time I turned me to the Angel and said,
“This discordant sound is that of many and diverse petitions, of
which some are directed to the Eternal but more to the Spirit of Evil. I
further perceive that well-nigh each and every voice thinks its own
tone the right and the only right tone, and some few voices there be
which desire all the others destroyed. Yet, I hear faintly a few that are
as pure and sweet as the voices of the morning stars when they sing
together.”
And the Angel said, “These are all the voices of the religions, the
sects, the churches, and the individual hearts, upon your planet.
They are many in number. They are wondrously many in number.
Yet, the understanding of your little heart is darkened: none of these
petitions are directed to the Spirit of Evil, though only God and we
know the heart of man, and the love of only God is great enough to
forgive your many strange desires. Those few and sweet voices—
ah! those few sweet voices redeem—redeem the world!”
As I listened again to the strange murmur I wept, and cried saying,
“Would that these voices were as one!”
And the Angel answered and said, “They will be when in that state
you call ‘Heaven.’”
Then did my soul face eagerly the face of the Angel and say to
him, “They will verily attain Heaven, then—all these many jangling
voices?”
Bending on me a wondering look he answered, “They will. All who
strive for Right and Light shall be happy. Worship they not all as truly
and deeply as they know? Strive they not all to love—to be unselfish,
although some half-heartedly? From the north and the south, from
the east and the west shall they be gathered, and there shall
carilloux harmonious ascend to The Eternal, as from one sweet and
glorified tongue.”
And as I listened again I sighed and said, “God is very patient.”
“God is very patient. He is Love, and his ways are past finding
out,” murmured the Angel.
Again he touched mine ears, saying, “Have you learned? Go,
return to earth, and live in the spirit of Love. Love, and judge not.
Love, and be very charitable, for you yourself jar on Heaven’s
peace.”
And I awoke, and beheld the impartial sun.
Charles P. Nettleton.
side talks with the philistines:
being soul easement and
wisdom incidentally.
The Sons of Melchizedek are a most peculiar people.
So far as I know the Society for Psychic Research has not yet
taken up the subject of these men who are without beginning of days
or end of time. Yet surely it is a most vital theme. These beings who
were never born and cannot die form a tribe that obeys no natural
edict—they are a law unto themselves. They appear and disappear,
like Clangingharp, to reappear again. Like vagrant comets, their orbit
cannot be determined.
They are visible only under peculiar and extraordinary conditions.
They materialize at will and disappear without explanation. After a
lecture I have seen one of them rush forward and greet the speaker
with a glow that must have gladdened the orator’s heart for months.
In fact, your son of Melchizedek is an orator himself and therefore
knows the orator’s need for a fervent word of appreciation after his
“effort.”
Once at a Methodist love feast, when there was a lull in the
program and the minister asked “And is there not just one more who
is willing to add a word of testimony?” I felt a slight cold feeling go
over me and knew at once that the Unknown was rising to his feet
behind. I heard him clear his throat and begin with “My friends, I
have been thinking while sitting here,” in a low, musical voice—a
voice all a-tremble with fervor. He spoke for fully fifteen minutes—
spoke with ease, and to the point. At first there was a craning of
necks and whispered questions as to who he was; but this was soon
lost in admiration. After the service he shook hands with many, then
disappeared, none knew where.
This mysterious being often helps to carry in the piano, and in
crowded street cars he has been known to supply the necessary
nickle when ladies could not find their pockets. When old gentlemen
fall in a fit on the street, he is on hand. Should a woman faint in
church, he gently carries her out. He opens the windows in cars, and
looks after the ventilator at all times, and at barbecues and outdoor
public meetings he calls up the stranger to the feast, introducing shy
countrymen to others still more shy, thus thawing the social ice and
making all secure.
When church debts are to be raised he sometimes arises in his
seat and subscribes a large amount. At mass meetings where
volunteers are called for to pass the hat he always responds. He
greets you cordially on the railway train, shaking hands as he
passes; asks after the wife and babies and shames you into smirking
idiocy because you cannot call him by name, and as he departs he
waves his hand and charges you thus: “Take care yourself, old man!”
He is always large, usually stout, and the true type has a dun
colored chin whisker. At least he should have.
He carries a glow of good nature that warms like wine; he is never
cast down, nor is his heart dismayed. At country funerals he often
appears, consoles the friends, takes care of the flowers, and
arranges the chairs in a column circle against the wall. At the
churchyard he walks with uncovered head by the side of the
clergyman, fetches the reins from the nearest team to lower the
coffin, and handles the shovel with an unction that savors of joy.
Surely, the Sons of Melchizedek, although not Philistines, are
Peculiar Persons.
She—“Don’t you think him rather thick-headed?”
He—(Who has lately been presented with a book of Familiar
Quotations) “Yes, indeed: his head is as thick as leaves in
Vallombrosa.”
The most biting bit of irony that passes as a current coin is that
reference to Boston as a city of culture—or it would be, were it not
for the insight shown in Paragraphs.
There were two Ballestiers. The good one, beloved of the gods
(and men), died young; but the one that resides near Brattleboro will
probably live to be a hundred. Rudyard benefitted the man and as a
sure result got his enmity. He is just literary enough in his instincts to
read his brother-in-law’s books for gibes and jeers at himself. He
imagines that every villain in the Kipling books is a black attempt to
paint a Beatty Ballestier portrait: not knowing that an author (like
Diety) creates in his own image. So the Ballestier reads and rages
and cuts hickory clubs and lies in wait for Mowgli, who dares not
venture out of sight of the bungalow except after dark. Has Rudyard
Kipling written the best he ever will? is a question that the bad
Ballestier proposes to answer by sending the soul of Kipling to join
that of Tomlinson. Some folks are saying “Good hunting to you, Kaa
Ballestier,” but Kipling in the meantime has had the rogue elephant
put under bonds to keep the peace, this as preliminary to the killing,
should the rogue continue to fool around the ’rickshaw.
In the story of “Kate Carnegie,” now running in The Bookman,
Ian Maclaren uses the exclamation “Dod!” about three times to a
page. This is understood to be a little advertising scheme suggested
by Mr. MacArthur. Your Scot is a very thrifty person.
Mr. Percival Pollard who fights in the Commissariat, and once
received a red badge, by being bumbasted with a pie (although he
swears ’twas a tart) is uttering rank heresy in the New York Journal
concerning Stephen Crane. The statement is made that Crane is
only a producer of “Bloomingdale symbolistic hash.” But now behold
Mr. Pollard has discovered Nankivell “King of Colorists and Past
Master of all living Draughtsmen”—bless my soul! when up pops a
writer in the Art Amateur and declares that the only fit man to
illustrate The Black Riders is this same Nankivell.
They do say that ’Enery James has resolved to write a novel
with an incident. Gosh!
Is it true that a folio periodical issued in Philadelphia is to have
a serial story entitled, “People Who Have Bored Me,” and that the
first chapters will be written by Mr. Howells, with comments on Scott,
Dickens, Thackeray, Dumas, Shakespeare, Moses, Phidias, the
critics of the daily papers and Henry Clay?
“Algernon wondered vaguely if he had done right in leaving the
handkerchief where she had dropped it, on the center table. If only it
had been the arm chair, now, what a different complexion it would
have put upon this dilemma—if it was a dilemma—no, hardly a
dilemma, rather a quandary, or even a question. And she—did she,
too, brood upon the handkerchief? Did she justify herself in dropping
it, in the way she did? He arose, wearily. ‘I dunno,’ he said, defiantly,
yet hopelessly, and sat down.”—Advance sheets from H. James.
That the Stage has fallen into a very bad way none dispute, and
under present conditions I cannot do better than to commend this
ordinance, passed in London in 1642, to the earnest consideration of
His Honor, the Mayor, and the Honorable Common Council of the
City of New York:
Little Journeys
SERIES FOR 1896
The above papers will form the series of Little Journeys for the
year 1896.
They will be issued monthly, beginning January, 1896, in the
same general style as the series of 1895, at 50 cents a year, and
single copies will be sold for 5 cents, postage paid.
G. P. PUTNAM’S SONS,
NEW YORK AND LONDON
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookultra.com