Python modules : os, subprocess and commands
1. What’s the difference between all of the os.popen() methods?
popen2 doesn't capture standard error, popen3 does capture standard
error and gives a unique file handle for it. Finally, popen4 captures
standard error but includes it in the same file object as standard
output.
os.popen() -> stdout
os.popen2() -> (stdin, stdout)
os.popen3() -> (stdin, stdout, stderr)
os.popen4() -> (stdin, stdout_and_stderr)
2. os.popen() vs os.system()
from what I've deciphered, the only differences between os.popen() and os.system() are
a) popen automatically starts a new process while system only does it if you include an &
b) popen hooks onto stdout and stdin.
If so, what's the use of os.system()???
a) is not true. Under Unix both start a new process.
The important difference (as I understand it) is popen lets you interact with the program by reading and writing pipes while it's running. system() is more of a batch mode thing, the program runs to completion, then you get the return status.
>??? If so, what's the use of os.system()?
If you just want to run a shell command and don't need to provide any stdin and don't care about stdout/stderr.
For example, if you just want to copy a directory tree from one place to another:
status = os.system("cp -r srcdir /some/where/else")
If you want to start a program, send it input and/or watch its output as it runs, then use popen().
os.system is a standard call to the C standard function call system().
It doesn't allow you to catch the output of the program like os.popendoes.
os.popen is piped open so that you can capture in your python scripts what the program outputs.
os.system simply calls the outside program and sends it's contents to stdout which can be
redirected to a file in your shell.
Call os.system if your python program doesn't need to capture anything from your outside program.
相关文档:
设计一个IP类:
设计1 要求:初始化时给定ip地址并判断ip地址的合法性
类方法:判断ip地址合法性
实例方法:将ip地址转化为10进制的表示形式及16进制的表示形式
......
在Stack Overflow 上看到学习Python 的一个方法是用Python 破解The Python Challenge。但我喜欢用Ruby,谁管得着呢^_^
0. 入门关很简单。
p 2**38
1. 破解一段话,观察图片很容易发现解码表把字母表循环右移两位。
riddle = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl ......
今晚试了一下Python自带的单元测试,主要是参考了Python单元测试框架
的有关资料,折腾了一个小时左右,总算在Eric 4通过的几个简单的单元测试。在这里将所得的相关知识记录下来,方便将来查询。
python自带的单元测试模块是unittest,从2.1以后为标准库的一部分
1 ......
Including binaries in your sources
+y F J2A T&N1H f(L ]0
Sometime it's handy to include small files in your sources (icons, test files, etc.)CNOUG博客首页 P x z c W R K+x3{ N
CNOUG博客首页 h I A O$k-n P
Let's take a file (myimage.gif) and convert it in base64 (optionnaly compressing it wit ......