python手册中文版 pdf免费,python参考手册中文版

大家好,小编为大家解答python手册中文版 pdf免费的问题。很多人还不知道python参考手册中文版,现在让我们一起来看看吧!

此文和python

内建函数 一样,内容全部出自python官方文档 ,但是会有自己的理解,并非单纯的翻译。所以,如果我理解有误,欢迎指正,谢谢快码论文

从python2.4版本开始,你就可以用可以用subprocess 这个模块来产生子进程,并连接到子进程的标准输入

/输出/错误中去,还可以得到子进程的返回值。subprocess 意在替代其他几个老的模块或者函数,比如:

os.system

os.spawn*

os.popen*

popen2.*

commands.*

下面将一一介绍如何用subprocess 来替代这些函数或者模块。

使用subprocess模块

本模块定义了一个类: Popen

classsubprocess .Popen (args , bufsize=0 , executable=None , stdin=None , stdout=None , stderr=None , preexec_fn=None ,close_fds=False , shell=False , cwd=None , env=None , universal_newlines=False , startupinfo=None , creationflags=0 )

各参数含义如下:

args 需要是一个字符串,或者包含程序参数的列表。要执行的程序一般就是这个列表的第一项,或者是字符串本身。但是也可以用executable 参数来明确指出。当executable 参数不为空时,args 里的第一项仍被认为是程序的“命令名”,不同于真正的可执行文件的文件名,这个“命令名”是一个用来显示的名称,例如执行*nix下的

ps 命令,显示出来的就是这个“命令名”。

在*nix下,当shell=False (默认)时,Popen使用os.execvp() 来执行子程序。args 一般要是一个列表。如果args 是个字符串的话,会被当做是可执行文件的路径,这样就不能传入任何参数了。

注意:

shlex.split() 可以被用于序列化复杂的命令参数,比如:

>>>

import

shlex

, subprocess

>>>

command_line = raw_input

(

)

/bin/vikings -input

eggs.txt

-output "spam

spam.txt"

-cmd

"echo '$MONEY'"

>>>

args = shlex

.split

(

command_line)

>>>

print

args

[

'/bin/vikings'

, '-input'

, 'eggs.txt'

, '-output'

, 'spam

spam.txt'

, '-cmd'

, "echo

'$MONEY'"

]

>>>

p = subprocess

.Popen

(

args)

#

成功执行!

可以看到,空格分隔的选项(如-input)和参数(如eggs.txt)会被分割为列表里独立的项,但引号里的或者转义过的空格不在此列。这也有点像大多数shell的行为。

在*nix下,当shell=True 时,如果args 是个字符串,就使用shell来解释执行这个字符串。如果args 是个列表,则第一项被视为命令,其余的都视为是给shell本身的参数。也就是说,等效于:

Popen(

[

'/bin/sh'

, '-c'

, args[

0

]

, args[

1

]

, ...]

)

在windows下,Popen使用接受字符串参数的CreateProcess()来执行子程序。如果args 是个列表,它会被先用list2cmdline() 转换成字符串。

如果指定了bufsize 参数,作用就和内建函数open()一样:0表示不缓冲,1表示行缓冲,其他正数表示近似的缓冲区字节数,负数表示使用系统默认值。默认是0。

executable 参数指定要执行的程序。它很少会被用到:一般程序可以由args 参数指定。如果shell=True ,executable 可以用于指定用哪个shell来执行(比如bash、csh、zsh等)。*nix下,默认是

/bin/sh ,windows下,就是环境变量 COMSPEC 的值。windows下,只有当你要执行的命令确实是shell内建命令(比如dir ,copy 等)时,你才需要指定shell=True ,而当你要执行一个基于命令行的批处理脚本的时候,不需要指定此项。

stdin 、stdout 和stderr 分别表示子程序的标准输入、标准输出和标准错误。可选的值有PIPE (见下面的描述)或者一个有效的文件描述符(其实是个正整数)或者一个文件对象,还有None。如果是PIPE,则表示需要创建一个新的管道,如果是

None,不会做任何重定向工作,子进程的文件描述符会继承父进程的。另外,stderr 的值还可以是STDOUT (见下),表示子进程的标准错误也输出到标准输出。

如果把preexec_fn 设置为一个可调用的对象(比如函数),就会在子进程被执行前被调用。(仅限*nix)

如果把close_fds 设置成True,*nix下会在开子进程前把除了0、1、2以外的文件描述符都先关闭。在

Windows下也不会继承其他文件描述符。

如果把shell 设置成True,指定的命令会在shell里解释执行,这个前面已经说得比较详细了。

如果cwd 不是None,则会把cwd 做为子程序的当前目录。注意,并不会把该目录做为可执行文件的搜索目录,所以不要把程序文件所在目录设置为cwd 。

如果env 不是None,则子程序的环境变量由env 的值来设置,而不是默认那样继承父进程的环境变量。注意,即使你只在env里定义了某一个环境变量的值,也会阻止子程序得到其他的父进程的环境变量(也就是说,如果env里只有1项,那么子进程的环境变量就只有1个了)。例如:

>>>

subprocess

.Popen

(

'env'

, env={

'xxx'

:'123'

, 'yyy'

:'zzz'

}

)

<

subprocess.Popen

object

at 0xb694112c>

>>>

xxx=123

yyy=zzz

如果把universal_newlines 设置成True,则子进程的stdout和stderr被视为文本对象,并且不管是*nix的行结束符('/n' ),还是老mac格式的行结束符('/r' ),还是windows

格式的行结束符('/r/n' )都将被视为 '/n' 。

如果指定了startupinfo 和creationflags ,将会被传递给后面的CreateProcess() 函数,用于指定子程序的各种其他属性,比如主窗口样式或者是子进程的优先级等。(仅限Windows)

介绍完Popen的各参数,再来看下两个小东西:

subprocess .PIPE

一个可以被用于Popen的stdin 、stdout 和stderr 3个参数的特输值,表示需要创建一个新的管道。

subprocess .STDOUT

一个可以被用于Popen的stderr 参数的特输值,表示子程序的标准错误汇合到标准输出。

方便的函数

subprocess .call (*popenargs , **kwargs )

执行命令,并等待命令结束,再返回子进程的返回值。参数同Popen,因为打开

/usr/lib/python2.6/subprocess.py 你就知道,去掉文档,其实是这样的:

def

call(

*

popenargs, **

kwargs)

:

return

Popen(

*

popenargs, **

kwargs)

.wait

(

)

subprocess .check_call (*popenargs , **kwargs )

执行上面的call命令,并检查返回值,如果子进程返回非0,则会抛出CalledProcessError 异常,这个异常会有个returncode 属性,记录子进程的返回值。

>>>

subprocess

.check_call

(

'false'

)

Traceback (

most recent call last)

:

File ""

, line 1

, in

<

module>

File "/usr/lib/python2.6/subprocess.py"

, line 498

, in

check_call

raise

CalledProcessError(

retcode, cmd

)

subprocess

.CalledProcessError

: Command 'false'

returned non-zero exit status 1

异常

子进程里抛出的异常,会在父进程中再次抛出。并且,异常会有个叫child_traceback 的额外属性,这是个包含子进程错误traceback信息的字符串。

遇到最多的错误回是

OSError,比如执行了一个并不存在的子程序就会产生OSError。

另外,如果使用错误的参数调用Popen,会抛出ValueError。

当子程序返回非0时,check_call()还会产生CalledProcessError 异常。

安全性

不像其他的popen函数,本模块不会偷偷地调用/bin/sh来解释命令,也就是说,命令中的每一个字符都会被安全地传递到子进程里。

Popen对象

Popen对象有以下方法:

Popen .poll()

检查子进程是否已结束,设置并返回 returncode 属性。

Popen .wait()

等待子进程结束,设置并返回 returncode 属性。

注意: 如果子进程输出了大量数据到stdout或者stderr的管道,并达到了系统

pipe的缓存大小的话,子进程会等待父进程读取管道,而父进程此时正wait着的话,将会产生传说中的死锁,后果是非常严重滴。建议使用communicate() 来避免这种情况的发生。

Popen .communicate (input =None )

和子进程交互:发送数据到stdin,并从stdout和stderr读数据,直到收到EOF。等待子进程结束。可选的input 如有有的话,要为字符串类型。

此函数返回一个元组: (stdoutdata , stderrdata )

注意,要给子进程的stdin发送数据,则Popen的时候,stdin要为PIPE;同理,要可以收数据的话,stdout或者stderr也要为

PIPE。

注意:读到的数据会被缓存在内存里,所以数据量非常大的时候要小心了。

Popen .send_signal (signal )

给子进程发送signal 信号量。

注意:windows下目前只支持发送SIGTERM,等效于下面的terminate() 。

Popen .terminate()

停止子进程。Posix下是发送SIGTERM信号。windows下是调用TerminateProcess() 这个API。

Popen .kill()

杀死子进程。Posix下是发送SIGKILL信号。windows下和terminate() 无异。

Popen .stdin

如果stdin 参数是PIPE,此属性就是一个文件对象,否则为None 。

Popen .stdout

如果stdout 参数是PIPE,此属性就是一个文件对象,否则为None 。

Popen .stderr

如果stderr 参数是PIPE,此属性就是一个文件对象,否则为None 。

Popen .pid

子进程的进程号。注意,如果shell 参数为True,这属性指的是子shell的进程号。

Popen .returncode

子程序的返回值,由poll()或者wait()设置,间接地也由communicate()设置。

如果为None,表示子进程还没终止。

如果为负数-N的话,表示子进程被N号信号终止。(仅限*nux)

用subprocess来代替其他函数

在这节里,举一些常用的例子,都可以用subprocess来完成,我们假定是用 “from subprocess import *”

来导入模块的:

代替shell命令:

output=`mycmd myarg`

等效于

output = Popen(["mycmd", "myarg"],

stdout=PIPE).communicate()[0]

代替shell管道:

output=`dmesg | grep hda`

等效于

p1 = Popen(["dmesg"], stdout=PIPE)

p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)

output = p2.communicate()[0]

代替os.system()

sts = os.system(“mycmd” + ”

myarg”)

等效于

p = Popen(“mycmd” + ” myarg”, shell=True)

sts = os.waitpid(p.pid, 0)[1]

注意:

通常并不需要用shell来调用程序。用subprocess可以更方便地得到子程序的返回值。

其实,更真实的替换是:

try:

retcode = call(“mycmd” + ” myarg”, shell=True)

if retcode < 0:

print >>sys.stderr, “Child was

terminated by signal”, -retcode

else:

print >>sys.stderr, “Child returned”,

retcode

except OSError, e:

print >>sys.stderr, “Execution

failed:”, e

代替os.spawn系列

P_NOWAIT的例子

pid = os.spawnlp(os.P_NOWAIT,

“/bin/mycmd”, “mycmd”, “myarg”)

等效于

pid = Popen(["/bin/mycmd", "myarg"]).pid

P_WAIT的例子

retcode = os.spawnlp(os.P_WAIT,

“/bin/mycmd”, “mycmd”, “myarg”)

等效于

retcode = call(["/bin/mycmd", "myarg"])

Vector的例子

os.spawnvp(os.P_NOWAIT, path,

args)

等效于

Popen([path] + args[1:])

关于环境变量的例子

os.spawnlpe(os.P_NOWAIT,

“/bin/mycmd”, “mycmd”, “myarg”, env)

等效于

Popen(["/bin/mycmd", "myarg"], env={“PATH”: “/usr/bin”})

代替os.popen() , os.popen2() , os.popen3() :

pipe = os.popen(“cmd”, ‘r’,

bufsize)

等效于

pipe = Popen(“cmd”, shell=True, bufsize=bufsize,

stdout=PIPE).stdout

pipe = os.popen(“cmd”, ‘w’,

bufsize)

等效于

pipe = Popen(“cmd”, shell=True, bufsize=bufsize,

stdin=PIPE).stdin

(child_stdin, child_stdout) =

os.popen2(“cmd”, mode, bufsize)

等效于

p = Popen(“cmd”, shell=True, bufsize=bufsize, stdin=PIPE,

stdout=PIPE, close_fds=True)

(child_stdin, child_stdout) = (p.stdin, p.stdout)

(child_stdin, child_stdout,

child_stderr) = os.popen3(“cmd”, mode, bufsize)

等效于

p = Popen(“cmd”, shell=True, bufsize=bufsize, stdin=PIPE,

stdout=PIPE, stderr=PIPE, close_fds=True)

(child_stdin, child_stdout, child_stderr) = (p.stdin, p.stdout,

p.stderr)

(child_stdin,

child_stdout_and_stderr) = os.popen4(“cmd”, mode,

bufsize)

等效于

p = Popen(“cmd”, shell=True, bufsize=bufsize, stdin=PIPE,

stdout=PIPE, stderr=STDOUT, close_fds=True)

(child_stdin, child_stdout_and_stderr) = (p.stdin, p.stdout)

*nix下,os.popen2, os.popen3,

os.popen4 也可以接受一个列表做为执行的命令,这时参数会被直接传给程序,而不经过shell的解释转换。如下:

(child_stdin, child_stdout) =

os.popen2(["/bin/ls", "-l"], mode, bufsize)

等效于

p = Popen(["/bin/ls", "-l"], bufsize=bufsize, stdin=PIPE,

stdout=PIPE)

(child_stdin, child_stdout) = (p.stdin, p.stdout)

返回值处理:

pipe = os.popen(“cmd”,

‘w’)

rc = pipe.close()

if rc != None and rc % 256:

print “There were some errors”

等效于

process = Popen(“cmd”, ‘w’, shell=True, stdin=PIPE)

process.stdin.close()

if process.wait() != 0:

print “There were some errors”

代替popen2 模块里的函数:

(child_stdout, child_stdin) =

popen2.popen2(“somestring”, bufsize, mode)

等效于

p = Popen(["somestring"], shell=True, bufsize=bufsize, stdin=PIPE,

stdout=PIPE, close_fds=True)

(child_stdout, child_stdin) = (p.stdout, p.stdin)

*nix下,popen2

也可以接受一个列表做为执行的命令,这时参数会被直接传给程序,而不经过shell的解释转换。如下:

(child_stdout, child_stdin) =

popen2.popen2(["mycmd", "myarg"], bufsize, mode)

等效于

p = Popen(["mycmd", "myarg"], bufsize=bufsize, stdin=PIPE,

stdout=PIPE, close_fds=True)

(child_stdout, child_stdin) = (p.stdout, p.stdin)

popen2.Popen3 and popen2.Popen4 基本上也能用

subprocess.Popen 代替,除了以下几点要注意:

执行失败的时候Popen会抛出异常capturestderr 参数用stderr 代替stdin=PIPE 和 stdout=PIPE 必须要指定popen2默认会关掉所有文件描述符,而Popen要指定close_fds=True

Python is an extensible, interpreted, object-oriented programming language. It supports a wide range of applica- tions, from simple text processing scripts to interactive Web browsers. Python 是一种可扩展的, 即译式, 面向对象规格的编程语言. 它能应用在极广泛的地方, 从简单的文字处理 工作到交互式的网页浏览器. While the Python Reference Manual describes the exact syntax and semantics of the language, it does not describe the standard library that is distributed with the language, and which greatly enhances its immediate usability. This library contains built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as modules written in Python that provide standardized solutions for many problems that occur in everyday programming. Some of these modules are explicitly designed to encourage and enhance the portability of Python programs. Python 语言参考手册 中精确地描述了Python 语言的句法及语义. 然而语言参考手册中没有提到Python 所 附带功能强大的标准库. 这个函式库大大地增强了Python 的实用性. 其中包括C 写的内建模组, 提供介面 让程式进行操作系统层次的工作, 例如档案的输出输入; 同时也有以Python 语言本身编写的模组, 为实际 编程时常遇的问题提供标准解决方案. 这类模组有的经过特别设计以便Python 程式在跨平台的情况下运 行无误. This library reference manual documents Python’s standard library, as well as many optional library modules (which may or may not be available, depending on whether the underlying platform supports them and on the configuration choices made at compile time). It also documents the standard types of the language and its built-in functions and exceptions, many of which are not or incompletely documented in the Reference Manual. 本参考手册罗列并说明了Python 标准库的各种功能, 以及许多非核心的模组(按不同的操作系统和编译时 的设置而定, 不是每台机上的Python 都能用这些模组.) 本手册同时记载了Python 语言所有的标准数据类 型, 内建函数, 异常类, 这些在参考手册中被忽略了或只是扼要的提过一下. This manual assumes basic knowledge about the Python language. For an informal introduction to Python, see the Python Tutorial; the Python Reference Manual remains the highest authority on syntactic and semantic questions. Finally, the manual entitled Extending and Embedding the Python Interpreter describes how to add new extensions to Python and how to embed it in other applications. 本手册的读者要对Python 有基本的认识. 初学者应该从Python 指南 开始. 至于Python 语言参考手册 则是该语言的语法和语义问题上的权威阐释. 最后 扩展或嵌入 Python 解释器 一文解说了如何在Python 中加入新的扩展模组; 以及怎样把Python 解释器嵌入到其他的应用程式中.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值