我對python很陌生。目標是使用子進程解析&從shell中檢索打印輸出來執行shell命令。執行錯誤如下面的示例輸出msg中所示。還示出了下面是示例代碼段在Python中轉義轉義序列
代碼段:
testStr = "cat tst.txt | grep Location | sed -e '/.*Location: //g' "
print "testStr = "+testStr
testStrOut = subprocess.Popen([testStr],shell=True,stdout=subprocess.PIPE).communicate()[0]
輸出:
testStr = cat tst.txt | grep Location | sed -e '/.*Location: //g'
cat: tst.txt: No such file or directory
sed: -e expression #1, char 15: unknown command: `/'
有一種解決方法,或者可以使用的功能?
感謝您的幫助 感謝
給予子過程文件「tst.txt」的完整路徑。 – MBarsi 2011-05-26 09:45:28