- 论坛徽章:
- 0
|
本帖最后由 xyz_hh 于 2012-01-29 15:31 编辑
看下面例子,执行cat file命令(file文件不存在),shell中的返回值是1,而python的commands.getstatusoutput返回值是256,如何才能获得和shell中一致的返回值?
[root@myssl ~]# cat file
cat: file: 没有那个文件或目录
[root@myssl ~]# echo $?
1
[root@myssl ~]#
[root@myssl ~]# python
Python 2.4.3 (#1, Jun 11 2009, 14:09:37)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import commands
>>> commands.getstatusoutput('cat file')
(256, 'cat: file: \xc3\xbb\xd3\xd0\xc4\xc7\xb8\xf6\xce\xc4\xbc\xfe\xbb\xf2\xc4\xbf\xc2\xbc')
>>>
|
|