1、文件系统 -- os, os.path, shutil
os 和os.path模块包含许多和文件系统交互的函数,shutil 模块 可以复制文件。
- filenames = os.listdir(dir) -- list of filenames in that directory path (not including . and ..). The filenames are just the names in the directory, not their absolute paths.(列出指定目录下所有文件的文件名,不包括路径)
- os.path.join(dir, filename) -- given a filename from the above list, use this to put the dir and filename together to make a path(将目录路径和文件名拼接合成文件路径)
- os.path.abspath(path) -- given a path, return an absolute form, e.g. /home/nick/foo/bar.html(返回绝对路径)
- os.path.dirname(path), os.path.basename(path) -- given dir/foo/bar.html, return the dirname "dir/foo" and basename "bar.html"(分别返回目录名 和 文件名)
- os.path.exists(path) -- true if it exists(判断路径是否存在)
- os.mkdir(dir_path) -- makes one dir(创建一个目录), os.makedirs(dir_path) makes all the needed dirs in this path(创建目录,尤其是连续创建多层目录时)
- shutil.copy(source-path, dest-path) -- copy a file (dest path directories should exist)(复制文件,目标路径必须存在)
## Example pulls filenames from a dir, prints their relative and absolute pathsdef printdir(dir): filenames = os.listdir(dir) for filename in filenames: print filename ## foo.txt print os.path.join(dir, filename) ## dir/foo.txt (relative to current dir) print os.path.abspath(os.path.join(dir, filename)) ## /home/nick/dir/foo.txt
2、执行外部进程命令
The *commands* module is a simple way to run an external command and capture its output.(command 模块提供了方便的方法来执行外部命令并捕获其输出)
- (command 模块文档)
- (status, output) = commands.getstatusoutput(cmd) -- runs the command, waits for it to exit, and returns its status int and output text as a tuple. The command is run with its standard output and standard error combined into the one output text. The status will be non-zero if the command failed. Since the standard-err of the command is captured, if it fails, we need to print some indication of what happened.
- output = commands.getoutput(cmd) -- as above, but without the status int.
- There is a commands.getstatus() but it does something else, so don't use it -- dumbest bit of method naming ever!
- If you want more control over the running of the sub-process, see the "popen2" module (http://docs.python.org/lib/module-popen2.html)
- There is also a simple os.system(cmd) which runs the command and dumps its output onto your output and returns its error code. This works if you want to run the command but do not need to capture its output into your python data structures.
## Given a dir path, run an external 'ls -l' on it --## shows how to call an external programdef listdir(dir): cmd = 'ls -l ' + dir print "Command to run:", cmd ## good to debug cmd before actually running it (status, output) = commands.getstatusoutput(cmd) if status: ## Error case, print the command's output to stderr and exit sys.stderr.write(output) sys.exit(1) print output ## Otherwise do something with the command's output
3、异常捕获及处理
try: ## Either of these two lines could throw an IOError, say ## if the file does not exist or the read() encounters a low level error. f = open(filename, 'rU') text = f.read() f.close() except IOError: ## Control jumps directly to here if any of the above lines throws IOError. sys.stderr.write('problem reading:' + filename) ## In any case, the code then continues with the line after the try/except
4、HTTP -- urllib and urlparse(http 处理)
## Version that uses try/except to print an error message if the## urlopen() fails.def wget2(url): try: ufile = urllib.urlopen(url) if ufile.info().gettype() == 'text/html': print ufile.read() except IOError: print 'problem reading url:', url