system : Centos 6.7 Lasted
Shell : bash
python : 2.6.6
這讓我非常困惑!下面的例子:混合python和bash shell的xargs命令疑惑
5個文件:
a1111 a2222 b1111 b2222 t.py
t.py內容:
import sys
if __name__ == '__main__':
a1 = sys.argv[1]
print 'id(a1)=%s, len(a1)=%s, str(a1)=%s, type(a1)=%s' % (id(a1), len(a1), str(a1), type(a1))
爲此,如:
ls | xargs -I{} echo $(python t.py '{}')
輸出:
id(a1)=139821454683184, len(a1)=2, str(a1)=a1111, type(a1)=<type 'str'>
id(a1)=139821454683184, len(a1)=2, str(a1)=a2222, type(a1)=<type 'str'>
id(a1)=139821454683184, len(a1)=2, str(a1)=b1111, type(a1)=<type 'str'>
id(a1)=139821454683184, len(a1)=2, str(a1)=b2222, type(a1)=<type 'str'>
id(a1)=139821454683184, len(a1)=2, str(a1)=t.py, type(a1)=<type 'str'>
我的問題是,爲什麼LEN(A1)= 2,但STR(A1)= a1111?,字符串長度明顯不等於2,
沒有迴音是好的,但它不是我的問題。我有使用xargs的-p選項打印CMD
ls | xargs -I{} python t.py '{}'
轉載於OS X上python 2.7.10和3.5.1 – cdarke