博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 常用标准库
阅读量:7033 次
发布时间:2019-06-28

本文共 3640 字,大约阅读时间需要 12 分钟。

hot3.png

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

 

转载于:https://my.oschina.net/u/2440318/blog/825720

你可能感兴趣的文章
day20 Python 装饰器
查看>>
限制性与非限制性定语从句区别
查看>>
fiddler工具的使用
查看>>
jquery源码分析(二)——架构设计
查看>>
javascript深入理解js闭包(转)
查看>>
207. Course Schedule
查看>>
如何优化您的 Android 应用 (Go 版)
查看>>
Trie树实现
查看>>
Opencv无法调用cvCaptureFromCAM无法打开电脑自带摄像头
查看>>
Exception异常处理机制
查看>>
复杂的web---web中B/S网络架构
查看>>
编写文档的时候各种问题
查看>>
Eclipse里maven的project报Unbound classpath variable: 'M2_REPO/**/***/***.jar
查看>>
新旅程CSS 基础篇分享一
查看>>
查看内核函数调用的调试方法【原创】
查看>>
个人项目中遇到的问题
查看>>
byte与base64string的相互转化以及加密算法
查看>>
20145103 《Java程序设计》第3周学习总结
查看>>
ubuntu声音系统
查看>>
哈哈更新资源列表2
查看>>