一、文件系統 -- os, os.path, shutil
os 和os.path模塊包含許多和文件系統交互的函數,shutil 模塊 能夠複製文件。html
- os 模塊文檔
- 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 paths
def 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
二、執行外部進程命令
The *commands* module is a simple way to run an external command and capture its output.(command 模塊提供了方便的方法來執行外部命令並捕獲其輸出)python
- commands module docs(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 program
def 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
三、異常捕獲及處理
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
四、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