os.system('cat /proc/cpuinfo') 无法获得到输出和返回值的
output = os.popen('cat /proc/cpuinfo')
print output.read() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出
distance = (dist.read()).replace("\n", ",").split(',')[-2] 可以通过replace()和split()函数对file 进行分段和提取,可以获得输出和返回值,(如果是合并file可以用.join()函数进行整合)
(status, output) = commands.getstatusoutput('cat /proc/cpuinfo')
print status, output 可以获得输出和返回值
python replace() 方法:
把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
replace()方法语法:
str.replace(old, new[, max])
- old -- 将被替换的子字符串。
- new -- 新字符串,用于替换old子字符串。
- max -- 可选字符串, 替换不超过 max 次
返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串,如果指定第三个参数max,则替换不超过 max 次。
例如:123hex_number=((hex_file.read()).replace("\n","")).split(", ")for i in hex_number: print int(i,16)
python 中的字符和数字之间的转换:
int(x [,base ]) 将x转换为一个整数
long(x [,base ]) 将x转换为一个长整数
float(x ) 将x转换到一个浮点数
complex(real [,imag ]) 创建一个复数
str(x ) 将对象 x 转换为字符串
repr(x ) 将对象 x 转换为表达式字符串
eval(str ) 用来计算在字符串中的有效Python表达式,并返回一个对象
tuple(s ) 将序列 s 转换为一个元组
list(s ) 将序列 s 转换为一个列表
chr(x ) 将一个整数转换为一个字符
unichr(x ) 将一个整数转换为Unicode字符
ord(x ) 将一个字符转换为它的整数值
hex(x ) 将一个整数转换为一个十六进制字符串
oct(x ) 将一个整数转换为一个八进制字符串